Linux Newbie help
 help / color / mirror / Atom feed
* C question
@ 2009-10-08  2:12 Rick Brown
  2009-10-08  2:52 ` Manish Katiyar
  2009-10-08  3:02 ` mayur nande
  0 siblings, 2 replies; 6+ messages in thread
From: Rick Brown @ 2009-10-08  2:12 UTC (permalink / raw)
  To: kernelnewbies, linux-newbie

Hello list,

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++"? But then how about this:

[rick@linux rick]$ cat t.c
#include <stdio.h>
int main()
{
    void *ptr = 0;
    printf("%d \n", ptr+1);
}
[rick@linux rick]$ gcc t.c
[rick@linux rick]$ ./a.out
1
[rick@linux rick]$

It compiles and runs fine ... !

TIA,

Rick

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C question
  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
  2009-10-08  3:02 ` mayur nande
  1 sibling, 2 replies; 6+ messages in thread
From: Manish Katiyar @ 2009-10-08  2:52 UTC (permalink / raw)
  To: Rick Brown; +Cc: kernelnewbies, linux-newbie

On Thu, Oct 8, 2009 at 7:42 AM, Rick Brown <rick.brown.3@gmail.com> wrote:
> Hello list,
>
> 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++"? But then how about this:

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.

/tmp> gcc a.c
a.c: In function ‘main’:
a.c:5: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘void *’
a.c:6: warning: dereferencing ‘void *’ pointer
a.c:6: error: invalid use of void expression
/tmp> cat a.c
#include <stdio.h>
int main()
{
   void *ptr = 0;
   printf("%d \n", ptr+1);
   printf("%d \n", *(ptr+1));
}


>
> [rick@linux rick]$ cat t.c
> #include <stdio.h>
> int main()
> {
>    void *ptr = 0;
>    printf("%d \n", ptr+1);
> }
> [rick@linux rick]$ gcc t.c
> [rick@linux rick]$ ./a.out
> 1
> [rick@linux rick]$
>
> It compiles and runs fine ... !
>
> TIA,
>
> Rick
>
> --
> To unsubscribe from this list: send an email with
> "unsubscribe kernelnewbies" to ecartis@nl.linux.org
> Please read the FAQ at http://kernelnewbies.org/FAQ
>
>



-- 
Thanks -
Manish
==================================
[$\*.^ -- I miss being one of them
==================================

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C question
  2009-10-08  2:12 C question Rick Brown
  2009-10-08  2:52 ` Manish Katiyar
@ 2009-10-08  3:02 ` mayur nande
  2009-10-08  5:43   ` sandeep lahane
  1 sibling, 1 reply; 6+ messages in thread
From: mayur nande @ 2009-10-08  3:02 UTC (permalink / raw)
  To: Rick Brown; +Cc: kernelnewbies, linux-newbie

[-- Attachment #1: Type: text/plain, Size: 1137 bytes --]

Hi Rick,

Some days ago i had the same question in my mind. While going through "The
Linux Kernel Architecture" book (by Wolfgang Mauerer), i got the answer:

The GNU compiler supports arithmetic with void pointers as well as function
pointers. The increment step is 1 byte. These are used by the kernel at
various points.

Have fun.

Regards
Mayur

On Thu, Oct 8, 2009 at 7:42 AM, Rick Brown <rick.brown.3@gmail.com> wrote:

> Hello list,
>
> 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++"? But then how about this:
>
> [rick@linux rick]$ cat t.c
> #include <stdio.h>
> int main()
> {
>    void *ptr = 0;
>    printf("%d \n", ptr+1);
> }
> [rick@linux rick]$ gcc t.c
> [rick@linux rick]$ ./a.out
> 1
> [rick@linux rick]$
>
> It compiles and runs fine ... !
>
> TIA,
>
> Rick
>
> --
> To unsubscribe from this list: send an email with
> "unsubscribe kernelnewbies" to ecartis@nl.linux.org
> Please read the FAQ at http://kernelnewbies.org/FAQ
>
>

[-- Attachment #2: Type: text/html, Size: 1715 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C question
  2009-10-08  2:52 ` Manish Katiyar
@ 2009-10-08  5:37   ` Kalpesh Rathod
  2009-10-08  8:48   ` Michał Nazarewicz
  1 sibling, 0 replies; 6+ messages in thread
From: Kalpesh Rathod @ 2009-10-08  5:37 UTC (permalink / raw)
  To: Rick Brown; +Cc: kernelnewbies, linux-newbie

Hi Rick,

gcc can warn about void pointer increment if you use compiler option
-Wpointer-arith

==
Kalpesh

On Thu, Oct 8, 2009 at 8:22 AM, Manish Katiyar <mkatiyar@gmail.com> wrote:
> On Thu, Oct 8, 2009 at 7:42 AM, Rick Brown <rick.brown.3@gmail.com> wrote:
>> Hello list,
>>
>> 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++"? But then how about this:
>
> 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.
>
> /tmp> gcc a.c
> a.c: In function ‘main’:
> a.c:5: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘void *’
> a.c:6: warning: dereferencing ‘void *’ pointer
> a.c:6: error: invalid use of void expression
> /tmp> cat a.c
> #include <stdio.h>
> int main()
> {
>   void *ptr = 0;
>   printf("%d \n", ptr+1);
>   printf("%d \n", *(ptr+1));
> }
>
>
>>
>> [rick@linux rick]$ cat t.c
>> #include <stdio.h>
>> int main()
>> {
>>    void *ptr = 0;
>>    printf("%d \n", ptr+1);
>> }
>> [rick@linux rick]$ gcc t.c
>> [rick@linux rick]$ ./a.out
>> 1
>> [rick@linux rick]$
>>
>> It compiles and runs fine ... !
>>
>> TIA,
>>
>> Rick
>>
>> --
>> To unsubscribe from this list: send an email with
>> "unsubscribe kernelnewbies" to ecartis@nl.linux.org
>> Please read the FAQ at http://kernelnewbies.org/FAQ
>>
>>
>
>
>
> --
> Thanks -
> Manish
> ==================================
> [$\*.^ -- I miss being one of them
> ==================================
>
> --
> To unsubscribe from this list: send an email with
> "unsubscribe kernelnewbies" to ecartis@nl.linux.org
> Please read the FAQ at http://kernelnewbies.org/FAQ
>
>
--
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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C question
  2009-10-08  3:02 ` mayur nande
@ 2009-10-08  5:43   ` sandeep lahane
  0 siblings, 0 replies; 6+ messages in thread
From: sandeep lahane @ 2009-10-08  5:43 UTC (permalink / raw)
  To: mayur nande; +Cc: Rick Brown, kernelnewbies, linux-newbie

[-- Attachment #1: Type: text/plain, Size: 1695 bytes --]

On Thu, Oct 8, 2009 at 8:32 AM, mayur nande <mayur.nan@gmail.com> wrote:

> Hi Rick,
>
> Some days ago i had the same question in my mind. While going through "The
> Linux Kernel Architecture" book (by Wolfgang Mauerer), i got the answer:
>
> The GNU compiler supports arithmetic with void pointers as well as function
> pointers. The increment step is 1 byte. These are used by the kernel at
> various points.
>
> Have fun.
>
> Regards
> Mayur
>
> On Thu, Oct 8, 2009 at 7:42 AM, Rick Brown <rick.brown.3@gmail.com> wrote:
>
>> Hello list,
>>
>> 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++"? But then how about this:
>>
>> [rick@linux rick]$ cat t.c
>> #include <stdio.h>
>> int main()
>> {
>>    void *ptr = 0;
>>    printf("%d \n", ptr+1);
>> }
>> [rick@linux rick]$ gcc t.c
>> [rick@linux rick]$ ./a.out
>> 1
>> [rick@linux rick]$
>>
>> It compiles and runs fine ... !
>>
>> TIA,
>>
>> Rick
>>
>> --
>> To unsubscribe from this list: send an email with
>> "unsubscribe kernelnewbies" to ecartis@nl.linux.org
>> Please read the FAQ at http://kernelnewbies.org/FAQ
>>
>>
>
Arithmetic on void and function pointers is part of GNU C extensions, C
standard
does not support it (size of void and functions is taken as 1). There are
many such
GNU extensions which are used in kernel. One way to figure out which all
extensions
are used is by providing -pedantic flag, this will emit warning for such
usage.

I think many of these extensions have became part of C99 standard already.


Regards,
Sandeep.

[-- Attachment #2: Type: text/html, Size: 2678 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C question
  2009-10-08  2:52 ` Manish Katiyar
  2009-10-08  5:37   ` Kalpesh Rathod
@ 2009-10-08  8:48   ` Michał Nazarewicz
  1 sibling, 0 replies; 6+ messages in thread
From: Michał Nazarewicz @ 2009-10-08  8:48 UTC (permalink / raw)
  To: Manish Katiyar, Rick Brown; +Cc: kernelnewbies, linux-newbie

> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2009-10-08  8:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2009-10-08  3:02 ` mayur nande
2009-10-08  5:43   ` sandeep lahane

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox