64bits and a problem with pointers

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

 



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,
I am using Slackware 13.0 64bits in a AMD Athlon 4400 64 x2
and, i found something interesting with C and GCC, look at this:

file1.c
/**********/
int main(void)
{
    char *p = 0;
    p = (char *)getpointer();
    return 0;
}
/***********/


file2.c
/***********/
void *getpointer(void)
{
    return 0x1234567887654321;
}
/***********/

can you imagine that if you compile those 2 files

gcc -c file1.c
gcc -c file2.c
gcc file1.o file2.o

and run it, the valued assigned to p is DIFFERENT from 0x1234567887654321? actually, in my
machine p gets the value 0xffffffff87654321

why? because getpointer() was NOT declared in file1.c, you fix this "issue" by adding
void *getpointer(void);
at the beginning of file1.c

why does this happen? because when gcc doesn't find the function getpointer(), I think it
"declares" it something like this
int getpointer(...);
and you are NOT warned that you forgot to declare the function (well, it actually tells
you, if you compile with the -Wall option)
so, the value returned by getpointer() is casted to int, losing 32bits, and then assigned
to p. With a 32bits architecture, ints and pointers are both 32bits long, so there is no
problem. Also, the same happens with long, double, and any other bigger-than-int.

if the getpointer() function returned, for example, char *, and you don't use the (char *)
while calling it, gcc warns you that you are assigning an int value to a char *.

The question is... is this a 100% programmer problem? or should gcc warn you also when you
don't use the -Wall option?



Thanks for reading,
Saludos!

- --
"Do you pine for the nice days of minix-1.1, when men were men and wrote their own device
drivers?" -- Linus Torvalds
"If you want to go somewhere, goto is the best way to get there." --  Ken Thompson
"Value your freedom or you will lose it" -- Richard Stallman

GNU/Linux Registered User #460377
Slackware 13.0 64 bits

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkz4NJcACgkQOLozpb7LE6J+XACgu0ZXD7x2GlxKBZPtUpTf20gs
TPsAoJyPQI/oh3OWuN2UOaHppP0Durl/
=bT9x
-----END PGP SIGNATURE-----


[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