Afficher un message
Vieux 19/10/2007, 02h48   #3
Jack Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Missing prototype and resulting coredump

On Thu, 18 Oct 2007 06:31:17 -0000, Rakesh UV <uvrakesh@gmail.com>
wrote in comp.lang.c:

> Hi,
> If i am not putting the function prototype of a function
> returning a pointer, i get a core dump.Though this will happen less
> probably on 32 bit machine
>
> example
> int main(int argc , char **argv)
> {
> char *base = basename(argv[0]) ;


If your compiler does not issue a diagnostic for this code, either it
is badly broken, or you did not tell it to operate as an actual C
compiler. A diagnostic is required.

> printf("%s",base);
> return 0;
>
> }
>
> The compiler assumes that basename is returning an int and thus we
> loose 32 bit of the actual address, because pointer is 64 bit and
> integer is 32 bit in 64 bit machines


And what if 64 bit addresses are returned in a completely different
manner and you are really losing all 64 bits of the address?


> I am working on
> Os linux x86_64 2.6.9
> Machine Hp
> compiler gcc 3.46
>
> Is there any option in GCC to make the default return value as long so


Why? The code has undefined behavior. It is wrong. It is garbage.

> that i can preserve the actual address returned
> or


What if 64 bit long long is returned in a different register than a 64
bit pointer?

> do we have any other way in C to make it right


There is no way in C to make it right. The code has undefined
behavior. If a function returns anything other than an int, and you
do not have a prototype in scope, they there is no way to avoid
undefined behavior. Period. The only way to make it right is to have
a prototype in scope. Period.

> I know that putting the prototype would solve the problem, but
> unfortunately
> there are huge number of files


That's why there are programs that can search and replace in huge
numbers of files.

> Rakesh UV


Don't EVER port code this bad to another platform without fixing it.

And reeducate the programmers who wrote it. If they won't accept
reeducation, fire them.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
  Réponse avec citation
 
Page generated in 0,06462 seconds with 9 queries