Πέμπτη, Οκτωβρίου 15, 2009

From 32bit to 64bit: void* to int gcc error

Yesterday I encountered the following problem. I was writing a program on a 32bit OS (Linux) and then I tried to compile the same source code on a 64bit Linux. The compilation failed because:

error: cast from ‘void*’ to ‘int’ loses precision

The reason: On 32bit compatible processors void* and int have the same size (32bit) but on 64bit ones void* is 64 bits and int 32 bits long.

The solution: Cast void* to long int.


Source:
http://seokjoon.springnote.com/pages/1688622.xhtml

2 σχόλια:

adamo είπε...

That is why you need to learn the size types, like uint32_t, int64_t, etc. See for example the uint32_t(3) manpage.

Panagiotis Bouros είπε...

:-)