0%

How to figure out `cast from pointer to integer of different size`

When I used cmocka to write the unit test, the compiler alerts a error cast from pointer to integer of different size [-Werror=pointer-to-int-cast]. I have tried to write a unit code for running arm 32-bit and x64 machine however it was not working since I wrote the following code that was only working properly on x64.

1
assert_false(list_find(list, 11));

Let’s figure out the problem since the void pointer.

First try, I rewrite the code on arm 32-bit that convert the void pointer to integer. So that it was working on my raspberry. However it was not working on x64 machine. WTF.

1
assert_false((int)list_find(list, 11));

Since the size of void pointer on x64 is 64-bit and the size of integer is 32-bit. Now the compiler alerts me another error message. How to figure out the problem. Let’s use a type intptr_t. The magic that converts to suitable size on the platform. If you compile the code on x64, it will convert to unsigned long that is 64-bit. Otherwise it converts to int.

1
assert_false((intptr_t)list_find(list, 11));