简体   繁体   中英

Storing an address into a function pointer

I was watching this video about calling the bootloader using software by assigning the address of the beginning of the system memory into a function pointer and then calling it, the expression for storing the address :

sysMemBootJump = (void(*)(void))(*(u32*)0x1fff0004);

"sysMemBootJump" is the function pointer.

But what I don't understand is, why did he dereference the address of the memory before casting it to void(*)(void) ?

Because the 'real' entry point is stored at that address. Think of it as being a pointer-to-pointer-to-function, by dereferencing you get just the pointer-to-function.

This is equivalent to:

u32 ad = *(u32*)0x1fff0004;

this is fetching a word located at address 0x1fff0004

then

sysMemBootJump = (void(*)(void))ad;

So 0x1fff0004 is the address of a word containing the routine's address.

And notice that the code is very unportable. A more portable integral type castable to/from addresses is uintptr_t from <stdint.h> ....

You can typedef function signatures, like here , to write more readable code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM