Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调用并使用c将数据复制到内存区域_C_Memory Management_Calloc - Fatal编程技术网

调用并使用c将数据复制到内存区域

调用并使用c将数据复制到内存区域,c,memory-management,calloc,C,Memory Management,Calloc,我试图分配一块内存,然后将数据复制到该空间。我做了这个简单的程序,但它没有达到我期望的效果。有人能指出我的错误推理吗 谢谢 #include <stdio.h> #include <stdlib.h> void main(void) { int t1 = 11; int t2 = 22; int *bufptr; bufptr = calloc(2, sizeof(int)); if(bufptr == NULL) { fprintf(stderr, "Out

我试图分配一块内存,然后将数据复制到该空间。我做了这个简单的程序,但它没有达到我期望的效果。有人能指出我的错误推理吗

谢谢

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

void main(void)
{
int t1 = 11;
int t2 = 22;
int *bufptr;

bufptr = calloc(2, sizeof(int));
if(bufptr == NULL)
{
    fprintf(stderr, "Out of memory, exiting\n");
    exit(1);
}

memcpy(bufptr, &t1, sizeof(int));
memcpy((bufptr+sizeof(int)), &t2, sizeof(int));

printf("bufptr11: %d\n", *bufptr);
printf("bufptr22: %d\n", *bufptr+sizeof(int));
}
printf(“bufptr22:%d\n”,*(bufptr+sizeof(int));

编辑:比帕伦夫妇更阴险的是(一开始我显然没有注意到)你实际上做了太多的指针运算。编译器已将bufptr+x调整为bufptr+(x*sizeof(int))字节。因此,当您执行
bufptr+sizeof(int)
时,您实际上超出了分配的内存

您可以通过以下方式看到这一点:

printf("bufptr: %p\n", bufptr);
printf("bufptr + sizeof(int): %p\n", bufptr + sizeof(int));
例如,对于me(32位机器),它输出:

bufptr: 0x876f008
bufptr + sizeof(int): 0x876f018
相隔16个字节,我只分配了8个字节!比
bufptr[1]
更有用的提醒

这是一种可怕的错误,一开始通常不会表现出来,然后在修改不相关的代码时开始导致崩溃。我会抓住它的

printf("bufptr22: %d\n", *bufptr+sizeof(int));
这被解释为(*bufptr)+sizeof(int)。这给了你11+4,使你看到的15

printf("bufptr22: %d\n", *(bufptr+sizeof(int)));
如果你想拿回22,那就是你想要的。

试试这些:

memcpy(bufptr, &t1, sizeof(int)); // copy 11 into the first element of the array.
memcpy(bufptr+1, &t2, sizeof(int)); // bufptr+1 will point to the next int of the array.

printf("bufptr11: %d\n", *bufptr);
printf("bufptr22: %d\n", *(bufptr+1)); // parenthesis needed as * has higher precedence than +
这:

是错误的,因为当您添加到指针时,您添加了指针指向的类型大小的单位。在本例中,
int
。因此无需涉及
sizeof
,只需直接使用“objects”(
int
s)的数量:

memcpy(bufptr + 1, &t2, sizeof(int));
此外,最好使用指针,而不是重复类型名,这样可以在将来更改时保护您:

memcpy(bufptr + 1, &t2, sizeof *bufptr);

在后一种情况下,使用目标指针的大小通常是一个好主意,因为如果源指针类型发生更改,它可以保护您(至少有一点)不被覆盖。它们当然应该是一样的,但仍然是。

非常有趣,我没有意识到这一点!谢谢你,马修!仅供参考,
main
函数返回
int
。总是。任何其他行为都可能导致未定义的行为。也许这就是你的问题
memcpy(bufptr + 1, &t2, sizeof(int));
memcpy(bufptr + 1, &t2, sizeof *bufptr);