Archive for November 6, 2012

GCC — 64bits addressing: function returns 32bits pointer

Today, it was the second time I stepped on an interesting problem with GCC  (well, it is actually a behavior). I created a function which returns a void* in a .c file. This C file was then compiled and added in a library (.a). When I used this function in an application, I was getting a void* pointer were the 32 most significant bits were either zeroed (0x00000000...) or set to 1 (0xFFFFFFFF...). My application is 64bit!!

For example, the debug prints I added would return:

[lib] allocated 0x7f756d6fa048
[app] allocated 0x6d6fa048

where you can see the “conversion”.

What was the problem? After some time of debugging, I realized that I had forgotten to include the aforementioned function in the corresponding header file :|. So, although GCC could find the function in the library I was linking the application to, I guess it was assuming a wrong return value/header for that function.

The conclusion: Be more careful 😉

PS. I am using the following version of GCC:

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' 
--with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ 
--prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib 
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix 
--with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ 
--enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes 
--enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror 
--with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu 
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)