All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Michał Nazarewicz" <m.nazarewicz@samsung.com>
To: Manish Katiyar <mkatiyar@gmail.com>, Rick Brown <rick.brown.3@gmail.com>
Cc: kernelnewbies <kernelnewbies@nl.linux.org>, linux-newbie@vger.kernel.org
Subject: Re: C question
Date: Thu, 08 Oct 2009 10:48:01 +0200	[thread overview]
Message-ID: <op.u1g32byw7p4s8u@localhost> (raw)
In-Reply-To: <ea11fea30910071952x58720190r9fef930ff524c849@mail.gmail.com>

> On Thu, Oct 8, 2009 at 7:42 AM, Rick Brown <rick.brown.3@gmail.com> wrote:
>> As far as I recall from K&R, isn't pointer arithmetic on a void
>> pointer banned? And any effort to do that results in an error -
>> because the compiler won't know by how much size to increment the
>> pointer for a statement like "ptr++"?

On Thu, 08 Oct 2009 04:52:31 +0200, Manish Katiyar <mkatiyar@gmail.com> wrote:
> But in the program, you aren't actually trying to dereference the
> value. Just adding means it becomes normal arithmetic and that is why
> you get result as 1. You will see the error if you try to dereference
> it.

This comment is a bit misleading.  The standard does not define behaviour
of pointer arithmetic on pointer to void.  What one need to realise is that
undefined behaviour means compiler's documentation may well define how such
a construct is evaluated and gcc (with proper options) decides to treat
pointer to void as if sizeof(void) == 1.

So the thing it's true pointer arithmetic on a pointer to void is undefined
behaviour as far as C standard is concerned however because Linux is
compiled with gcc kernel's developers tend to make use of gcc's extensions
and one of it is arithmetic on a pointer to void.

-- 
Best regards,                                           _     _
  .o. | Liege of Serenely Enlightened Majesty of       o' \,=./ `o
  ..o | Computer Science,  Michał "mina86" Nazarewicz     (o o)
  ooo +---<mina86@mina86.com>---<mina86@jabber.org>---ooO--(_)--Ooo--

--
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

  parent reply	other threads:[~2009-10-08  8:48 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-08  2:12 C question Rick Brown
2009-10-08  2:52 ` Manish Katiyar
2009-10-08  5:37   ` Kalpesh Rathod
2009-10-08  8:48   ` Michał Nazarewicz [this message]
2009-10-08  3:02 ` mayur nande
2009-10-08  5:43   ` sandeep lahane
  -- strict thread matches above, loose matches on Subject: below --
2010-05-25 10:08 C Question Randi Botse
2010-05-25 10:19 ` Xiaotian Feng
     [not found]   ` <AANLkTinTu94E7guvzjySpGjht2Hjw4aop4vRIVpHo9UL@mail.gmail.com>
2010-05-25 10:49     ` Randi Botse
2010-05-25 11:18 ` Uriel Corfa
2010-05-25 11:41 ` Michal Nazarewicz
2010-05-30 10:41   ` Randi Botse
2010-05-30 12:38     ` Michal Nazarewicz
2010-06-01 11:59       ` Randi Botse
2010-06-01 13:18         ` Glynn Clements
1999-10-21  1:40 C++ Question ian reinhart geiser

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=op.u1g32byw7p4s8u@localhost \
    --to=m.nazarewicz@samsung.com \
    --cc=kernelnewbies@nl.linux.org \
    --cc=linux-newbie@vger.kernel.org \
    --cc=mkatiyar@gmail.com \
    --cc=rick.brown.3@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.