C 同一个地址有不同的指针?
请解释为什么同一地址有两个不同的值事实上,这并没有发生 同时,以我的方式释放指针也会产生错误: *检测到glibc*/home/travis/build/batousik/Practical-C2/bin/.libs/lt-Practical_树:双重免费或损坏(fasttop):0x00000000005010***C 同一个地址有不同的指针?,c,arrays,pointers,int,dereference,C,Arrays,Pointers,Int,Dereference,请解释为什么同一地址有两个不同的值事实上,这并没有发生 同时,以我的方式释放指针也会产生错误: *检测到glibc*/home/travis/build/batousik/Practical-C2/bin/.libs/lt-Practical_树:双重免费或损坏(fasttop):0x00000000005010*** 在实际的程序中,我有int数组中的测试数据和树数据类型。我用数组中的一些数据填充树,然后尝试递归地释放树中节点和节点的每个值,但失败了。是不是因为节点中的所有值指针实际上都是一个
在实际的程序中,我有int数组中的测试数据和树数据类型。我用数组中的一些数据填充树,然后尝试递归地释放树中节点和节点的每个值,但失败了。是不是因为节点中的所有值指针实际上都是一个内存块?然后我是否应该执行类似于memcopy的操作,将数组中的值复制到为节点值分配的新内存空间中
calloc()
1返回的地址
int *ptr = calloc(10,(sizeof(int)));
int *ptr2 = (ptr);
for (int i = 0; i < 10; ++i) {
int r = rand() % 20000;
*(ptr + i) = r;
printf("[i:%d, v:%d, a:%p]", i, *(ptr + i), ptr+i);
fflush(stdout);
}
printf("\n");
printf("[v:%d, a:%p]", *(ptr2), ptr2);
printf("\n");
*ptr2=5;
printf("[v:%d, a:%p]", *(ptr2), ptr2);
printf("\n");
for (int i = 0; i < 10; ++i) {
printf("[i:%d, v:%d, a:%p]", i, *(ptr + i), ptr+i);
}
printf("\n");
free(ptr2);
free(ptr);
然后让ptr2
指向与
ptr = calloc(10, sizeof(int));
所以释放ptr
或ptr2
实际上是同一个指针ptr2 = ptr;
更改由ptr
或ptr2
指向的第一个元素的值,事实上,您总是在打印之前覆盖该地址的值free()
调用之前执行此操作
*ptr2 = 5;
将打印相同的值,因此实际上需要调用free()
传递ptr
或ptr2
中的任何一个,但传递后不传递另一个
1如果要在调用
calloc()
之后显式初始化数据,则无需使用calloc()
。另外,您应该检查calloc()
/malloc()
是否没有返回NULL
calloc()
1返回的地址
int *ptr = calloc(10,(sizeof(int)));
int *ptr2 = (ptr);
for (int i = 0; i < 10; ++i) {
int r = rand() % 20000;
*(ptr + i) = r;
printf("[i:%d, v:%d, a:%p]", i, *(ptr + i), ptr+i);
fflush(stdout);
}
printf("\n");
printf("[v:%d, a:%p]", *(ptr2), ptr2);
printf("\n");
*ptr2=5;
printf("[v:%d, a:%p]", *(ptr2), ptr2);
printf("\n");
for (int i = 0; i < 10; ++i) {
printf("[i:%d, v:%d, a:%p]", i, *(ptr + i), ptr+i);
}
printf("\n");
free(ptr2);
free(ptr);
然后让ptr2
指向与
ptr = calloc(10, sizeof(int));
所以释放ptr
或ptr2
实际上是同一个指针ptr2 = ptr;
更改由ptr
或ptr2
指向的第一个元素的值,事实上,您总是在打印之前覆盖该地址的值free()
调用之前执行此操作
*ptr2 = 5;
将打印相同的值,因此实际上需要调用free()
传递ptr
或ptr2
中的任何一个,但传递后不传递另一个
1如果要在调用
calloc()
之后显式初始化数据,则无需使用calloc()
。另外,您应该检查calloc()
/malloc()
是否没有返回NULL
calloc()
1返回的地址
int *ptr = calloc(10,(sizeof(int)));
int *ptr2 = (ptr);
for (int i = 0; i < 10; ++i) {
int r = rand() % 20000;
*(ptr + i) = r;
printf("[i:%d, v:%d, a:%p]", i, *(ptr + i), ptr+i);
fflush(stdout);
}
printf("\n");
printf("[v:%d, a:%p]", *(ptr2), ptr2);
printf("\n");
*ptr2=5;
printf("[v:%d, a:%p]", *(ptr2), ptr2);
printf("\n");
for (int i = 0; i < 10; ++i) {
printf("[i:%d, v:%d, a:%p]", i, *(ptr + i), ptr+i);
}
printf("\n");
free(ptr2);
free(ptr);
然后让ptr2
指向与
ptr = calloc(10, sizeof(int));
所以释放ptr
或ptr2
实际上是同一个指针ptr2 = ptr;
更改由ptr
或ptr2
指向的第一个元素的值,事实上,您总是在打印之前覆盖该地址的值free()
调用之前执行此操作
*ptr2 = 5;
将打印相同的值,因此实际上需要调用free()
传递ptr
或ptr2
中的任何一个,但传递后不传递另一个
1如果要在调用
calloc()
之后显式初始化数据,则无需使用calloc()
。另外,您应该检查calloc()
/malloc()
是否没有返回NULL
calloc()
1返回的地址
int *ptr = calloc(10,(sizeof(int)));
int *ptr2 = (ptr);
for (int i = 0; i < 10; ++i) {
int r = rand() % 20000;
*(ptr + i) = r;
printf("[i:%d, v:%d, a:%p]", i, *(ptr + i), ptr+i);
fflush(stdout);
}
printf("\n");
printf("[v:%d, a:%p]", *(ptr2), ptr2);
printf("\n");
*ptr2=5;
printf("[v:%d, a:%p]", *(ptr2), ptr2);
printf("\n");
for (int i = 0; i < 10; ++i) {
printf("[i:%d, v:%d, a:%p]", i, *(ptr + i), ptr+i);
}
printf("\n");
free(ptr2);
free(ptr);
然后让ptr2
指向与
ptr = calloc(10, sizeof(int));
所以释放ptr
或ptr2
实际上是同一个指针ptr2 = ptr;
更改由ptr
或ptr2
指向的第一个元素的值,事实上,您总是在打印之前覆盖该地址的值free()
调用之前执行此操作
*ptr2 = 5;
将打印相同的值,因此实际上需要调用free()
传递ptr
或ptr2
中的任何一个,但传递后不传递另一个
1如果要在调用
calloc()
之后显式初始化数据,则无需使用calloc()
。另外,您应该检查calloc()
/malloc()
是否没有返回NULL
这只是复制指针的值。在这一行之后,ptr
和ptr2
都指向相同的分配内存。那么
int *ptr2 = (ptr);
您将释放相同的分配两次
我不确定你的代码应该做什么。有两个循环都在覆盖同一内存块。出于某种原因,您还在该块的第一个int
中写入5
这只是复制指针的值。Af