This function is buried in a complex nest so actually finding the cause is probably beyond anything I can ask, but I'm wondering if anyone might be able to give some tips on how I might go about debugging this. Here is the gist of the code I'm having trouble with
//func1.c
somestruct* func1(somestruct* mystruct)
{
printf("func1: %p, %i\n", mystruct, mystruct->foo);
return mystruct;
}
//func2.c
somestruct* func1(somestruct* mystruct);
void func2()
{
somestruct *mystruct = malloc(sizeof(somestruct));
mystruct->foo = 10;
printf("func2: %p, %i\n", mystruct, mystruct->foo);
mystruct = func1(mystruct);
printf("back in func2: %p\n", mystruct);
free(mystruct);
}
And I call func2. The output is like so
func2: 0x7f38a00008c0, 10
func1: 0x7f38a00008c0, 10
back in func2: 0xffffffffa00008c0
(SEGFAULT trying to free 0xffffffffa00008c0)
The actual code is more complex, "mystruct" gets passed around in many other places as well without issue, the fact that the functions are in different files seems like it might be part of the problem, yes it needs to return the pointer (the returned pointer is not guaranteed to be the same as the input pointer). It seems really weird to me that it's kind of (but not actually) getting truncated to 32 bits, and then filled with ffffffff at the top.
When compiled on a 32 bit machine it works exactly as it should.
I'd considered memory corruption somewhere, so I ran it through valgrind. Valgrind reports no errors, and in fact causes it to complete successfully. Textbook heisenbug. At least I can use GDB.
Does anyone have any idea what might be causing this, or at least how I might start tracking down the problem?
Size of a Pointer to Pointer So, the size of a pointer to a pointer should have the usual values, that is, 2 bytes for a 16-bit machine, 4 bytes for a 32-bit machine, and 8 bytes for a 64-bit machine.
If the system is 32-bit, size of void pointer is 4 bytes. If the system is 64-bit, size of void pointer is 8 bytes.
The size of the pointer basically depends on the architecture of the system in which it is implemented. For example the size of a pointer in 32 bit is 4 bytes (32 bit ) and 8 bytes(64 bit ) in a 64 bit machines. The bit types in a machine are nothing but memory address, that it can hold.
Here's why it matters Simply put, a 64-bit processor is more capable than a 32-bit processor because it can handle more data at once. A 64-bit processor can store more computational values, including memory addresses, which means it can access over 4 billion times the physical memory of a 32-bit processor.
Check your code if you missed out function prototype (somestruct* func1(somestruct* mystruct);).in func2.c.
By default all return values are int. So if a prototype is missing for function then compiler treats the return value as 32-bit and generates code for 32-bit return value. Thats when your upper 4 bytes gets truncated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With