Pointer being cast 32-bit in function return

Joined
Apr 19, 2010
Messages
1
Reaction score
0
Points
1
Good morning,

I have some C code which I'm trying to port to OS X (Snow Leopard) and I'm having trouble with a pointer I'm returning getting truncated to 32-bits when it's returned from a call.

The prototype for the callee is:

helix_sites* get_helices(char*, char*, char*, char*);

Where helix_sites is previously defined as a struct:

typedef struct {
int helix_count;
struct helix_sites *helix;
} helix_sites;

In the callee function get_helices I successfully make a call to malloc:

helix_sites* hSites;
....
hSites = (helix_sites *) malloc_or_die(sizeof (helix_sites));
....
return hSites;

In ddd I see the pointer is 64-bit, I successfully use the new structure, all is fine. However when I return this pointer to the caller which accepts the pointer in its own helix_sites* variable the
pointer somehow gets truncated to 32-bits. I can see this as well when checking with ddd, the upper 32-bits of the pointer are suddenly missing.

The compiler gives a warning:

cdriver.c:24: warning: cast to pointer from integer of different size

I can only guess this has something to do with how the return value is passed on the stack, that a 64-bit slot isn't allocated since both the caller and callee are using 64-bit pointers for the
variable.

I've tried swapping in void* pointers as a test then casting it to the helix_sites* type, no luck with that either, which definitely says its on the compiler/runtime side.

Any advice would be welcome, if there are any known issues with returning pointers in C compiling with gcc under Snow Leopard or such.

Thanks.
 
Joined
Jun 25, 2009
Messages
50
Reaction score
4
Points
8
Location
Plaistow, W.Sx. UK
Your Mac's Specs
iMac (Retina 5K, 27-inch, 2019) 3GHz 6-Core Intel Core i5 16GB, Sonoma 14.2.1
Hi Antarctican

What is the actual code at line 24 in cdriver.c?

cdriver.c:24: warning: cast to pointer from integer of different size

implies that, although the compiler can compile the code, it is doubtful that you will get the result you want/expect.

The compiler is saying that you are implicitly casting from an integer to a pointer. Is the prototype definition correct?

Can you post a bit more of the code, especially line 24, and the variable definitions please?

Cheers

Smudger
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top