我有一大块内存,我将这些内存的一部分传递给这些函数:
void setBlockSize(char* node, int size) {
printf("need size: %i\n",size);
memcpy(node, (void *)&size, sizeof(size));
printf("set to: %i\n",node);
}
//written for 4 byte pointer, 32 bit addressing
void setNextPointer(char* node, char* next){
printf("need ptr: %p\n", next);
memcpy((node+4), (void*)&next, sizeof(next));
printf("next: %p, set: %p\n",next, (void*)(node+4));
}
我的输出如下:
need size: 8296
set to: 137666560
need ptr: (nil)
next: (nil), set: 0x834a004
need size: 137666456
set to: 137666560
need ptr: 0xffee4874
next: 0xffee4874, set: 0x834a004
need size: 104
zsh: segmentation fault (core dumped) ./mallocTest
似乎设置了错误的值(我正在尝试设置一个指针和一个整数。这对 memcpy 的用法不正确吗?