C question
am 08.10.2009 04:12:18 von Rick Brown
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
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
Re: C question
am 08.10.2009 04:52:31 von Manish Katiyar
On Thu, Oct 8, 2009 at 7:42 AM, Rick Brown 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 =91main=92:
a.c:5: warning: format =91%d=92 expects type =91int=92, but argument 2 has =
type =91void *=92
a.c:6: warning: dereferencing =91void *=92 pointer
a.c:6: error: invalid use of void expression
/tmp> cat a.c
#include
int main()
{
void *ptr =3D 0;
printf("%d \n", ptr+1);
printf("%d \n", *(ptr+1));
}
>
> [rick@linux rick]$ cat t.c
> #include
> int main()
> {
> =A0 =A0void *ptr =3D 0;
> =A0 =A0printf("%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
>
>
--=20
Thanks -
Manish
==================== =====3D=
=========3D
[$\*.^ -- I miss being one of them
==================== =====3D=
=========3D
--
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
Re: C question
am 08.10.2009 05:02:01 von mayur nande
--000e0cd5f674fef9fa047563af9e
Content-Type: text/plain; charset=ISO-8859-1
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 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
> 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
>
>
--000e0cd5f674fef9fa047563af9e
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Hi Rick,
Some days ago i had the same question in my mind. While goi=
ng through "The Linux Kernel Architecture" book (by Wolfgang Mau=
erer), i got the answer:
The GNU compiler supports arithmetic with v=
oid pointers as well as function pointers. The increment step is 1 byte. Th=
ese are used by the kernel at various points.
Have fun.
Regards
Mayur
On=
Thu, Oct 8, 2009 at 7:42 AM, Rick Brown
<
ailto:rick.brown.3@gmail.com">rick.brown.3@gmail.com> wrote:<=
br>
204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">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()
{
=A0 =A0void *ptr =3D 0;
=A0 =A0printf("%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
..org">ecartis@nl.linux.org
Please read the FAQ at
blank">http://kernelnewbies.org/FAQ
--000e0cd5f674fef9fa047563af9e--
--
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
Re: C question
am 08.10.2009 07:37:18 von Kalpesh Rathod
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 wro=
te:
> On Thu, Oct 8, 2009 at 7:42 AM, Rick Brown w=
rote:
>> 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 =91main=92:
> a.c:5: warning: format =91%d=92 expects type =91int=92, but argument =
2 has type =91void *=92
> a.c:6: warning: dereferencing =91void *=92 pointer
> a.c:6: error: invalid use of void expression
> /tmp> cat a.c
> #include
> int main()
> {
> =A0 void *ptr =3D 0;
> =A0 printf("%d \n", ptr+1);
> =A0 printf("%d \n", *(ptr+1));
> }
>
>
>>
>> [rick@linux rick]$ cat t.c
>> #include
>> int main()
>> {
>> =A0 =A0void *ptr =3D 0;
>> =A0 =A0printf("%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
> ==================== ===3D=
===========3D
> [$\*.^ -- I miss being one of them
> ==================== ===3D=
===========3D
>
> --
> 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
Re: C question
am 08.10.2009 07:43:32 von sandeep lahane
--001636499e49a02149047565f16f
Content-Type: text/plain; charset=ISO-8859-1
On Thu, Oct 8, 2009 at 8:32 AM, mayur nande 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 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
>> 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.
--001636499e49a02149047565f16f
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
On Thu, Oct 8, 2009 at 8:32 AM, mayur na=
nde
<mayur.nan@=
gmail.com> wrote:
=3D"border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; p=
adding-left: 1ex;">
Hi Rick,
Some days ago i had the same question in my mind. While goi=
ng through "The Linux Kernel Architecture" book (by Wolfgang Mau=
erer), i got the answer:
The GNU compiler supports arithmetic with v=
oid pointers as well as function pointers. The increment step is 1 byte. Th=
ese are used by the kernel at various points.
Have fun.
Regards
Mayur
t>On Thu, Oct 8, 2009 at 7:42 =
AM, Rick Brown
<
om" target=3D"_blank">rick.brown.3@gmail.com> wrote:
tyle=3D"border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8e=
x; padding-left: 1ex;">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()
{
=A0 =A0void *ptr =3D 0;
=A0 =A0printf("%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
..org" target=3D"_blank">ecartis@nl.linux.org
Please read the FAQ at
blank">http://kernelnewbies.org/FAQ
Arithmetic =
on void and function pointers is part of GNU C extensions, C standard
do=
es not support it (size of void and functions is taken as 1). There are man=
y such
GNU extensions which are used in kernel. One way to figure out which all ex=
tensions
are used is by providing -pedantic flag, this will emit warning=
for such usage.
I think many of these extensions have became part o=
f C99 standard already.
Regards,
Sandeep.
=A0
--001636499e49a02149047565f16f--
--
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
Re: C question
am 08.10.2009 10:48:01 von m.nazarewicz
> On Thu, Oct 8, 2009 at 7:42 AM, Rick Brown w=
rote:
>> 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 =
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 behavio=
ur
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 undef=
ined
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 extensi=
ons
and one of it is arithmetic on a pointer to void.
--=20
Best regards, _ _
.o. | Liege of Serenely Enlightened Majesty of o' \,=3D./ `o
..o | Computer Science, MichaÅ "mina86" Nazarewicz (o o)
ooo +---------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