Re: Arithmetic on function address

From: Stephen Biggs (Spammers_at_AreLowLifes.com-INVALID-MUNGED)
Date: 04/23/04


Date: Fri, 23 Apr 2004 19:51:39 +0000 (UTC)

Eric Sosman <Eric.Sosman@sun.com> wrote in
news:4089560E.986D134A@sun.com:

> Stephen Biggs wrote:
>>
>> Given this code:
>> void f(void){}
>> int main(void){return (int)f+5;}
>>
>> Is there anything wrong with this in terms of the standards?
>
> Yes and no. The Standard permits you to cast a pointer
> value (even a function pointer value) to an integer, but it
> does not guarantee that the result is useful or even usable.

But, it then should allow you to add a value to that integer, as the
code says, no? Is this what you mean by no guarantees of it being
usable?
 
>
>> Is this legal C code?
>
> Legal but useless.

Ok, fine... I agree completely that it is useless, but shouldn't correct
code be generated for it?
 
>
>> One compiler I'm working with compiles this quietly, even with the
>> most stringent and pedantic ANSI and warning levels, but generates
>> code that only loads the address of "f" and fails to make the
>> addition before returning a value from "main".
>>
>> GCC "does the right thing".
>>
>> Is there something I'm missing?
>
> Your marbles, perhaps. ;-) What are you trying to
> accomplish with this ill-defined operation?
>

Thank you for that :)... I am not trying to accomplish anything besides
running the GCC testsuite on some other compiler that I am trying to
analyze. This is part of the testsuite and passes with GCC, no
problem... I was just wondering if this is a bug in the compiler that I
am trying to run on this code? I want to be sure that this should
generate code correctly before I cry "bug". That is, that it should
generate code to add the constant after the pointer is converted to an
integer.

Thanks for any help.



Relevant Pages

  • Re: large files: when ubiquitous?
    ... I was merely pointing out that the compiler cannot ... > it should NEVER make optimizations that assume that it has unique ... > Then it's a bug. ... >> to a char pointer type or changing p to be a char ...
    (comp.os.linux.development.system)
  • Re: Arithmetic on function address
    ... The Standard permits you to cast a pointer ... garbage out" is the rule that applies here. ... You mean that on this compiler, ...
    (comp.lang.c)
  • Re: Function prefix comments in C files
    ... now that reminds me of a bug I spent an unreasonable time searching ... It turned out that when the compiler had its ... default optimization level on the access to the pointer was ... With optimization turned off access was no longer atomic and it ...
    (comp.arch.embedded)
  • Re: Linux "NULL pointer dereferece" in the News...
    ... > Rahul Sundaram wrote, ... This is not a compiler bug. ... I vaguely remember some of the reasons (mainly the difficulty of untangling the semantics of pointer tests), but, I must admit, after using Java long enough, it seems odd to me that the C compiler would be smart enough to catch the theoretical "don't care" and not smart enough to distinguish between the reasons for the don't care. ...
    (Fedora)
  • Re: [bug] crash when reading /proc/mounts (was: Re: Linux 2.6.23-rc9 and a heads-up for the
    ... You have a terminally buggy piece of shit compiler. ... BUG: unable to handle kernel paging request at virtual address f2a40000 ... if 'malloc' happened to return a pointer at the end of an allocation". ...
    (Linux-Kernel)