Re: C question

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

 



Hi,

On Thu, 8 Oct 2009 11:13:32 +0530, sandeep lahane
<sandeep.lahane@xxxxxxxxx> wrote:
> On Thu, Oct 8, 2009 at 8:32 AM, mayur nande <mayur.nan@xxxxxxxxx> 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@xxxxxxxxx>
>> 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@xxxxxxxxxxxx
>>> 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.

That's weird, Seems I'll need to bone up there too.
Dereferencing a void pointer is clearly defined as an illegal operation.
But I thought manipulating a void pointer was vendor specific (IOW not
clearly defined).

Personally, I don't even see the point in doing arithmetic on void
pointers.
The whole idea is to be able to typecast to different logical sizes, so
the pointer manipulation should use typecasts in this case.
So, arithmetic on void pointers is - as far as I'm concerned - bad
coding...

But that's my opinion. 

In analogy : a while ago I came across a tricky bug in uIP V1.00
uIP used 2 function calls' return result in one of its protothread
operations, BUT was relying on the calling order
of functions (their result) within that single line statement.
ISO (AFAIK) nowhere defines a compulsory "calling order". 
So this is a bug IMO.
The code was written on GCC, but I used it on MSP430 with CrossWorks
toolchain, and there the function calling order was reversed,
thus the uIP code was badly broken. (even though it's supposed to be ANSI
compliant).

I reported the bug to Adam but never got any feedback.

Surely this is strictly a standards issue, and not "a matter of opinion"
??

Best regards,
Kris

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



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux