gcc on 64 bit && different behaviour while 'cast to pointer from integer of different size'

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

 




Hello,

We compile on Solaris SPARC a big software project and for the first
time to support 64 bit. The historical grown C code was never written
with 64 bit in mind and on 32 bit pointers and integers are of the same size.

We have two different version of gcc available on this host, 3.4.3 and 4.4.2.
I have below a small example where a function fun() should return a char pointer
and due to its missing declaration as 'extern char *fun();' the compiler
treats the function as integer returning and produce the warning

   cast to pointer from integer of different size

Until now total clear. The questions are:

Why does the gcc 3.4.3 correct this problem on the fly and stores
the full 64 bit pointer into the receiving char pointer, while the
gcc 4.4.2 does not? 

When this feature or behaviour was changed after gcc 3.4.3?

Is this somehow a command line flag for gcc to control the behaviour?

For sure, the correct way is to change the (hundreds) of places where
the function declaration is missing.

Thanks

	matthias


$ uname -a
SunOS srap01dxle 5.10 Generic_150400-17 sun4u sparc SUNW,SPARC-Enterprise

$ /usr/local/bin/gcc --version
gcc (GCC) 4.4.2
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ /usr/sfw/bin/gcc --version
gcc (GCC) 3.4.3 (csl-sol210-3_4-branch+sol_rpath)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ cat main.c
/* main.c */
#include <stdio.h>

/* extern char* fun(); */

main(int argc, char **argv)
{
   char *p = (char *) NULL;
   p = (char *)  fun();
   printf("main has p as: %016lx\n", p);
   printf("main has p as: %s\n", p);
}

$ cat fun.c
/* fun.c */
#include <stdio.h>
char *fun()
{
   char *p = "fun";
   printf("fun() returns pointer p as: %016lx\n", p);
   return (p);
}

$ /usr/sfw/bin/gcc -m64 main.c fun.c ; ./a.out
main.c: In function `main':
main.c:9: warning: cast to pointer from integer of different size
fun() returns pointer p as: 0000000100000c80
main has p as: 0000000100000c80
main has p as: fun

$ /usr/local/bin/gcc -m64 main.c fun.c ; ./a.out
main.c: In function 'main':
main.c:9: warning: cast to pointer from integer of different size
fun() returns pointer p as: 0000000100000c30
main has p as: 0000000000000c30
Segmentation Fault (core dumped)

-- 
Matthias Apitz, guru@xxxxxxxxxxx, http://www.unixarea.de/  +49-176-38902045
No! Nein! ¡No! Όχι! -- Ευχαριστούμε!



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux