简体   繁体   English

将 memory 分配给 function 指针

[英]allocating memory to a function pointer

I have a func to add two nos and return (a+b).我有一个函数来添加两个 no 并返回 (a+b)。 Then I created a func pointer to the func.然后我创建了一个指向函数的函数指针。 Want to allocate memory for an array of that function pointer and access them.想要为该 function 指针的数组分配 memory 并访问它们。 code is below.代码如下。

My question is on the following line using malloc:我的问题是使用 malloc 的以下行:

pp = (add_2nos*)malloc(5 * sizeof(add_2nos*))

sizeof(add_2nos*) and sizeof(add_2nos) does not make any difference while compiling. sizeof(add_2nos*) 和 sizeof(add_2nos) 在编译时没有任何区别。 What is the difference if there is any??有的话有什么区别?? Also if type casting is necessary while I am allocating memory of the same type...?另外,如果在我分配相同类型的 memory 时需要进行类型转换...?

#include <stdio.h>
#include <stdlib.h>

int add(int a, int b) {
        return (a+b);
}

typedef int (*add_2nos)(int, int);     

int main() {

        add_2nos *pp; 
        
        // Defining an array of pointers to function add and accessing them
        pp = (add_2nos*)malloc(5 * sizeof(add_2nos*));
        pp[0] = add;
        pp[1] = add;
        printf("\n\nAdding two nos -- (14, 15): %d ", pp1[0](14, 15));
        printf("\nAdding two nos -- (16, 16): %d \n\n", pp1[1](16, 16));
}

sizeof(add_2nos*) and sizeof(add_2nos) does not make any difference while compiling. sizeof(add_2nos*) 和 sizeof(add_2nos) 在编译时没有任何区别。 What is the difference if there is any?如果有的话有什么区别?

add_2nos is void (*)(int, int) - it's a pointer to a function. add_2nosvoid (*)(int, int) - 它是指向 function 的指针。

add_2nos* is a void (**)(int, int) - it's a pointer, to a pointer to a function. add_2nos*是一个void (**)(int, int) - 它是一个指针,指向指向 function 的指针。

Because on most architectures all pointers have the same size, including function pointers and pointers to pointers, and malloc() just takes a number, it doesn't make a difference.因为在大多数架构上,所有指针都具有相同的大小,包括 function 指针和指向指针的指针,而malloc()只需要一个数字,它没有区别。 Anyway you want to allocate space for 5 function pointers (ie. 5 * sizeof(add_2nos) ), not for 5 pointers to function pointers.无论如何,您想为5 function 指针(即5 * sizeof(add_2nos) )分配空间,而不是为指向 function 指针的5指针分配空间。

Don't think about it and let the compiler figure it out: pp = malloc(5 * sizeof(*pp));别想了,让编译器自己算出来: pp = malloc(5 * sizeof(*pp)); is a common pattern.是一种常见的模式。

Also if type casting is necessary while I am allocating memory of the same type...?另外,如果在我分配相同类型的 memory 时需要进行类型转换...?

See do we cast the result of malloc in C .请参阅我们是否将 malloc 的结果转换为 C

This这个

pp = (add_2nos*)malloc(5 * sizeof(add_2nos*));

is giving you an allocation based on the size of a pointer to a function pointer.根据指向 function 指针的指针大小为您提供分配。 That's probably the same as a function pointer, but maybe not.这可能与 function 指针相同,但可能不是。

pp = malloc(5 * sizeof(add_2nos));

or或者

pp = malloc(5 * sizeof *pp);

You've typedef's add_2nos to be a typename for "pointer to function with a particular signature", so to allocate an array of such pointers, you just want to use sizeof on it directly (no dereference), or use sizeof on the dereference of the array pointer.您已将 typedef 的add_2nos用作“指向具有特定签名的 function 的指针”的类型名,因此要分配此类指针的数组,您只需对其直接使用 sizeof (不取消引用),或在取消引用时使用 sizeof数组指针。

Pointer determines memory address.指针确定 memory 地址。 The easiest way to explain this is by this example:解释这一点的最简单方法是通过以下示例:

printf("%ld\n",sizeof(unsigned char));
printf("%ld\n",sizeof(unsigned char *));

In the first line, the size of an unsigned char is printed, which is 1 byte.在第一行中,打印了一个 unsigned char 的大小,即 1 个字节。 In the second line, the size of a pointer which keeps the information, where unsigned char is stored in the memory.在第二行中,保存信息的指针的大小,其中 unsigned char 存储在 memory 中。 This may vary between compilers/platforms.这可能因编译器/平台而异。 For example, on https://www.onlinegdb.com/online_c_compiler size of 8 bytes is printed.例如,在https://www.onlinegdb.com/online_c_compiler上打印 8 个字节的大小。

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

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