Guye Incognito
Guye Incognito

Reputation: 2844

xcode build error: Semantic issue cast from pointer to smaller type 'int' loses information

I never use macs or xcode but I need to now in order to build a C++ library for osx and ios. The C++ code builds fine for windows and android but in xcode I get hundreds of these red excmalation mark icons indicating this:

 Semantic issue
 cast from pointer to smaller type
'int' loses information

Here is an example of a line of code indicating this error

    fixturesArray.push_back((int)fixture->GetUserData());

Ive already had an issue building with unused variables and I removed a flag -Werror and this fixed that particular issue. Is there something similar I can do for this issue? I'm assuming the issue is warnings being treated as errors. Here are the other flags that are still there..

-Wall  -Wno-long-long -Wno-variadic-macros     -DNDEBUG -DDEBUG=0   

UPDATE: There were only about 10 or so instances of that error (the other 100's of things were warnings) If I change 'int' to 'int64' in all those cases then I can build. But I dont want to do that, I'm sure it wall cause issues, that is not how the program is supposed to work.

UPDATE2: Also leaving the code the way it is and just removing these flags lets me build
-Wno-long-long

Upvotes: 0

Views: 4373

Answers (2)

pqnet
pqnet

Reputation: 6608

This code runs fine for windows because visual studio will create 32-bit binaries by default, and android is a 32-bit platform. In these platforms pointers are 32-bit, and int are 32 bit too, so it is possible to store a pointer in an int typed vector or variable.

Xcode will build by default both a 64-bit and 32-bit binary if you target Mac OS X, and the warning you get is actually telling you that on 64-bit platforms putting a pointer inside an int variable is not a good idea, because half of the pointer's value is lost, and you won't be able to retrieve it back later.

You can chase two different solutions in your case:

  • fix the code, changing int to intptr_t and fix whatever stops working because of that.
  • decide you don't want to fix it and so that you are not going to support 64-bit platform.

In the second case you can disable building a 64-bit binary and only use 32-bit ones on mac os by changing some options on xcode. To do this remove the x86_64 string from the "valid architectures" setting in target's "build settings" (if you can't find the setting, search google images for xcode valid architectures build setting)

Upvotes: 2

hfhc2
hfhc2

Reputation: 4421

Well, it seems that GetUserData() returns a pointer. This seems to have a sizeof(void*) > sizeof(int) so casting cuts something off

Edit: If it works with an int64 this confirms my assumption (you seem to be sitting in front of a 64 bit machine). I am not sure what you are trying to do but casting pointers to integers is a bad idea. I would suggest that you make the fixturesArray a container of a suitable value type if you want this to work more independtly of the architecture.

Upvotes: 0

Related Questions