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

时间:2021-12-02 19:33:02

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:

我想创建一个void函数指针数组,分配它的内存并分配给我希望它包含的函数的数组地址。在声明数组时,您可以使用括号中的列表形式为其分配元素,如下所示:

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上分配地址的“括号形式”,这样它实际指向任何函数?更一般的问题:有没有办法快速完成,或者我必须做很长的事情,如下所示:

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

3 个解决方案

#1


2  

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};

#2


1  

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数组上完成,然后复制到分配的数组。

#3


-1  

void (*pointerArray)(void) is a declaration of a function pointer. *pointerArray is a function. 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.

void(* pointerArray)(void)是函数指针的声明。 * pointerArray是一个函数。函数没有大小,你不能分配它们,并且没有函数数组这样的东西,你不能分配或复制函数。

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 };

#1


2  

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};

#2


1  

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数组上完成,然后复制到分配的数组。

#3


-1  

void (*pointerArray)(void) is a declaration of a function pointer. *pointerArray is a function. 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.

void(* pointerArray)(void)是函数指针的声明。 * pointerArray是一个函数。函数没有大小,你不能分配它们,并且没有函数数组这样的东西,你不能分配或复制函数。

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 };