Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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_Arrays_Pointers_Int_Dereference - Fatal编程技术网

C 同一个地址有不同的指针?

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数组中的测试数据和树数据类型。我用数组中的一些数据填充树,然后尝试递归地释放树中节点和节点的每个值,但失败了。是不是因为节点中的所有值指针实际上都是一个

请解释为什么同一地址有两个不同的值事实上,这并没有发生

同时,以我的方式释放指针也会产生错误:

*检测到glibc*/home/travis/build/batousik/Practical-C2/bin/.libs/lt-Practical_树:双重免费或损坏(fasttop):0x00000000005010***


在实际的程序中,我有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