将函数指针复制到 C 中的内存块

Copy Function Pointer to Block of Memory in C

我正在使用 pthreads API 实现一个线程库。我需要做的一件事是分配一个大小为 32767 字节的堆栈,然后将指向 pthread_exit 的函数指针放在堆栈的顶部,并将堆栈指针递减到该函数指针下方。

我想出的代码如下:

    void *stackPointer = malloc(32767);
    stackPointer += 32767;
    stackPointer -= sizeof(&pthread_exit);
    stackPointer = memcpy(stackPointer, pthread_exit, sizeof(&pthread_exit));

据我所知,以这种方式使用 memcpy 函数时,函数指针未正确放置在内存中。我应该用另一种方法来解决这个问题(或我应该使用的功能)吗?

编辑: 固定代码从一开始就在顶部有堆栈指针

所提供的代码有很多可疑之处。其中有

  • void *类型的指针做指针运算是non-conforming;
  • 您似乎正在丢弃指向 dynamically-allocated 块的唯一指针,这使得稍后释放该块变得棘手(如果可能的话);和
  • 根据您打算如何使用它,您可能无法确保在堆栈上推送的值正确对齐。

但这些相对较小:一些编译器用 void-pointer 算术做你想做的事情,你可以重新计算释放块所需的指针,也许对齐不是问题你的目的。

然而,

From what I can tell the function pointer is not properly placed in the memory by using the memcpy function in this way.

不,绝对不是。 memcpy()函数将源指针指向的数据复制到目标指针指示的位置。你想写指针本身,这完全是另一回事。

Is there another way I should be going about this (or function I should be using)?

您可以将指针存储在一个变量中,然后从那里复制它:

#define STACK_SIZE 32767

void (*pte)(void *) = pthread_exit;

void *stackLimit = malloc(STACK_SIZE);
void *stackBottom = stackLimit + STACK_SIZE;
void *stackPointer = (char *) stackBottom - sizeof(pte);

memcpy(stackPointer, pte, sizeof(pte));

或者如果未对齐访问不是问题(或者如果堆栈大小多于或小于 32767 都可以),那么您可以使用简单的赋值来更简单地完成它:

void *stackLimit = malloc(STACK_SIZE);
void *stackBottom = stackLimit + STACK_SIZE;
typedef void (*pte_type)(void *);

((pte_type) stackBottom)[-1] = pthread_exit;
void *stackPointer = (pte_type) stackBottom - 1;

typedef 是为了清晰和便于阅读。可以在没有的情况下编写等效代码。

注意,函数类型的表达式,例如 pthread_exit 是当该函数的声明在范围内时,会自动转换为函数指针。您不需要对它们使用 & 运算符,尽管这样做是无害的。