Re: C question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Oct 8, 2009 at 7:42 AM, Rick Brown <rick.brown.3@xxxxxxxxx> 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@xxxxxxxxx> 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@xxxxxxxxxx>---<mina86@xxxxxxxxxx>---ooO--(_)--Ooo--

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

[Index of Archives]     [Audio]     [Hams]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Fedora Users]

  Powered by Linux