简体   繁体   中英

What does void (* const ivt[])(void) mean?

I'm reading the book Embedded Systems Architecture by Daniele Lacamera. In Chapter 4,The Boot-Up code

__attribute__ ((section(".isr_vector"))) void (* const ivt[])(void) =
{
    (void (*) (void))(END_STACK),
    isr_reset,
    //...
};

I don't understand void (* const ivt[])(void) , is it the array? If it's the array what does (void) mean?

Install the tool cdecl and ask it to explain things to you:

mrvn@ryzen:~$ cdecl
Type `help' or `?' for help
cdecl> explain void (* const ivt[])(void)
declare ivt as array of const pointer to function (void) returning void

This is the declaration of an interrupt vector table for a microcontroller, likely an ARM Cortex M.

  • __attribute__ ((section(".isr_vector"))) is a non-standard gcc extension assigning the variable to a specific section in memory (absolute address). You'll find .isr_vector in the corresponding linker script.
  • Regarding void (* const ivt[])(void) :
    • void (*f)(void) would create a function pointer (to a function taking no arguments and returning no value - which is true for all interrupt service routines).
    • void (*f[])(void) would create an array of function pointers. Since no size of the array is specified, the size will be determined by the following initializer list.
    • void (* const ivt[])(void) adding the const keyword will make the array of function pointers read-only, which is a requirement since it should get allocated in flash and never changed in run-time.
  • (void (*) (void))(END_STACK) could cast an integer END_STACK to a function pointer. Alternatively this could also be a dirty, strictly speaking invalid cast from a struct or other object into a function pointer. The target appears to be an ARM core where the default stack pointer is stored at the bottom of the interrupt vector table, then then sp is set automatically by the hardware.
  • isr_reset is the first interrupt, the reset vector.

它是一个名为ivt的数组,包含指向不带参数并返回void的函数的常量指针。

i dot't understading void (* const ivt[])(void), is it the array?

Yes, ivt is an array of constant pointers to function that accepts no arguments and returns nothing.

The type of elements of ivt array is

 void (* const)(void)

which means constant pointer to function which accepts no arguments and returns nothing.

If it's the array what does (void) mean?

It means function accepts no arguments.

ivt is an array of constant pointers to functions that takes no parameters and have the return type void .

Perhaps an example might help understand the situation better:

void func1()
{
    std::cout<<"func1 called"<<std::endl;
}
void func2()
{
    std::cout<<"func2 called"<<std::endl;
}
void foo()
{
    std::cout<<"foo called"<<std::endl;
}

void foobar()
{
    
}
int main()
{
//----------------vvv---------------------------------> ivt is an array of const pointers to a function with return type of void and having 0 parameters
    void (* const ivt[])(void) = {func1, func2, foo};  
//--------------------------------^^^^^--^^^^^--^^^---> add pointers  to these 3 functions into the array 


   //iterate through the elements in the array and call the member functinos 
   for(void (*const PTR)(void): ivt)
   {
       PTR();
       
//-----------v---------------------------------------->this assignment won't work because pointers are themselves const so you can make them point to another function
       //PTR = foobar; 
   }
    
}

The output of the above program is:

func1 called
func2 called
foo called

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