简体   繁体   English

如何将内存分配给void函数指针的数组并为数组分配地址

[英]How to allocate memory to an array of void function pointers and assign adresses to the array

I want to create an array of void function pointers, allocate its memory and assign to the array adresses of function I want it to contain. 我想创建一个空函数指针数组,分配它的内存,并分配给我希望它包含的函数数组地址。 While declaring an array, you are able to assign to it elements in a form of list in brackets like this: 在声明数组时,您可以像这样在方括号中为列表分配元素:

const char *array[NUM_OF_ELEMENTS] = {"foo1", "boo2", "foo2", "boo2"}; // etc.

Alright. 好的。 I have declared an array of pointers to void functions as it is shown below: 我已声明一个指向void函数的指针数组,如下所示:

void (*pointerArray)(void) = malloc(NUM_OF_ELEMENTS * sizeof(*pointerArray));

My question is: Is it possible to first declare a function and simultaneously allocate its memory, and then use "bracket form" of assigning adresses on my pointerArray so it actually points to any function? 我的问题是: 是否可以先声明一个函数并同时分配其内存,然后使用在我的指针数组上分配地址的“括弧形式”,使其实际上指向任何函数? More general question: Is there any way to do it quick way or I have to do this the long way as shown here: 更笼统的问题:有什么办法可以快速完成此操作,或者我必须这样做很长,如下所示:

pointerArray[0] = func1;
pointerArray[1] = func2;
pointerArray[2] = func3; // etc.

With NUM_OF_ELEMENTS being a constant value, there doesn't seem to be any good reason for you to allocate this array dynamically, so you may as well allocate it statically and initialize it upon declaration: 由于NUM_OF_ELEMENTS为常数,似乎没有充分的理由来动态分配此数组,因此您也可以静态分配它并在声明时对其进行初始化:

typedef void(*func_ptr)(void);
func_ptr func_ptr_array[] = {func1,func2,func3};

You can use the stack: 您可以使用堆栈:

void (*p_stack[])(void) = { func1 , func2 } ;

Or in the case you really need to use malloc, first allocate your array with malloc and the another one on the stack and copy: 或者,在确实需要使用malloc的情况下,请先使用malloc分配数组,然后在堆栈上分配另一个数组并复制:

void (*p_stack[NUM_OF_ELEMENTS])(void) = { func1 , func2 } ;
void (**pointerArray)(void) = malloc(NUM_OF_ELEMENTS * sizeof(*pointerArray));
memcpy( pointerArray , p_stack , sizeof( p_stack) ) ;

The initialization is done on the p_stack array, and is then copied to the allocated array. 初始化在p_stack数组上完成,然后复制到分配的数组中。

void (*pointerArray)(void) is a declaration of a function pointer. void (*pointerArray)(void)是函数指针的声明。 *pointerArray is a function . *pointerArray是一个函数 Functions don't have sizes and you cannot allocate them and there is no such thing as an array of functions and you cannot assign or copy functions. 函数没有大小,您不能分配它们,也没有诸如函数数组之类的东西,您不能分配或复制函数。

What you need is an array of function pointers. 您需要的是一个函数指针数组。 To make things easier let's notate it with a typedef: 为了使事情变得更简单,我们用typedef标记它:

typedef void vfun(void);
typedef vfun* vfunp;

vfunp* pointerArray = malloc (NUM_OF_ELEMENTS * sizeof(vfunp));

pointerArray[0] = func1;
pointerArray[1] = func2;
pointerArray[2] = func3; // etc.

Or you can have a statically allocated array if you want: 或者,您可以根据需要使用静态分配的数组:

vfunp pointerArray[] = { func1, func2, func3 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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