Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are we adding 0 to a double void pointer here?

For context, this is code called from a bootloader that is supposed to boot into the main application. This snippet is from a function with an argument uintptr_t address that specifies the address of where the main application has been written to. I believe sp and pc are the stack pointer and program counter respectively.

Here's the code:

sp = *((void **)address + 0);
pc = *((void **)address + 1);
start_new_application(sp, pc);

Full context here

And then I've gone and printed the address, sp, and pc, and found the following:

address -> 0x08010000
sp ------> 0x20050000
pc ------> 0x080132DD

This has me very confused. I'm not sure why the sp line has a + 0 in it at all. Could that sp line be rewritten as:

sp = (void *)address;

and do the same thing?

My understanding is that the address has been static_casted into a double void pointer and then sp is given the dereferenced value of address (0x08010000) and pc has been given the dereferenced value of the address (0x08010001). Then, in my printf statements these values displayed are the dereferenced values of those addresses.

Is my understanding correct here?

like image 419
Capn Jack Avatar asked Jul 29 '26 00:07

Capn Jack


1 Answers

The + 0 is there merely for readability.

sp = *((void **)address + 0);
pc = *((void **)address + 1);

does the same as

sp = *((void **)address);
pc = *((void **)address + 1);

or using C++ casts and the subscript operator:

sp = reinterpret_cast<void**>(address)[0];
pc = reinterpret_cast<void**>(address)[1];

Could that sp line be rewritten as:

sp = (void *)address;

No, that would make sp equal to address which is not what the original code does. The orignal code extracts a void* from the address pointed to: *address.

like image 73
Ted Lyngmo Avatar answered Jul 30 '26 15:07

Ted Lyngmo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!