【问题标题】:C freeing array isn't workingC释放数组不起作用
【发布时间】:2015-04-14 01:45:29
【问题描述】:

这是我的代码。我正在创建一个数组,给元素一些值,然后释放和打印。

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

int main()
{

    // malloc the array
    int* array = (int*) malloc(sizeof(int)*3);

    // give some values
    int i;
    for(i=0; i<3; i++) array[i] = 1 + i*i;

    // attempt to free 
    free(array+0);
    free(array+1);
    free(array+2);

    // print array elements
    for(i=0; i<3; i++) printf("%d , ", array[i]);
    return 0;
}

它只释放第一个和第二个元素(打印随机数),但第三个元素保留在那里。我该怎么做才能解决这个问题?如果我做错了什么(例如错误地使用免费),请也指出这一点。谢谢。

【问题讨论】:

  • 这是错误的,正如 zenith 所指出的那样,但即使不是这样,取消引用 freed 指针听起来像是未定义的行为
  • 一个free()malloc(),您释放的空间是预期的三倍。 malloc/free 处理内存区域,他们不知道您如何构建这些区域。 malloc 需要知道区域的大小,但该表达式在实际调用之前会被评估(产生一个数字)。如果你做一个malloc,你只需要做一个free(用你从malloc收到的指针作为参数)。

标签: c arrays malloc free


【解决方案1】:

您不能一个一个地释放数组的元素。

您必须将与malloc 提供给您的完全相同的指针传递给free

【讨论】:

  • 不要投malloc()
  • 谢谢大家。仍然得到相同的结果。
  • @FeloVilches 在释放 array 后访问它是未定义的行为。 IE。在那之后,你的价值观可能存在,也可能不存在。
  • @zenith 好吧,我是否应该相信该数组已被释放,即使正在打印一些原始值? (我根据您的建议更改了代码)
  • @FeloVilches 是的。 free 不会以任何方式覆盖已释放的数据。
【解决方案2】:

C 在后台跟踪分配的内存。如果要释放已分配的块,则需要传入指向该内存块开头的指针。您应该使用:

free(array);

另外,你不应该使用 malloc。

【讨论】:

    【解决方案3】:

    free(array+0) 已释放整个sizeof(int)*3 数组,以下两个free() 调用将导致未定义的行为(请参阅http://www.cplusplus.com/reference/cstdlib/free/)。

    void free (void* ptr);

    释放内存块

    一块内存 先前通过调用 malloc、calloc 或 realloc 分配的是 解除分配,使其再次可用于进一步分配。

    如果ptr没有指向上面分配的内存块 函数,它会导致未定义的行为。

    如果 ptr 是空指针,则函数什么也不做。

    【讨论】:

      【解决方案4】:

      另外,请注意,在 free()ed 之后访问内存区域会导致未定义的行为。 What happens to the data in memory deallocated by free()?

      因此,您不能通过打印存储在已释放的内存区域中的值来真正得出结论。

      【讨论】:

        【解决方案5】:

        显示正确的代码:

        #include <stdio.h>
        
        int main()
        {
        
            // malloc the array
            int* array = (int*) malloc(sizeof(int)*3);
        
            // give some values
            int i;
            for(i=0; i<3; i++) 
                array[i] = 1 + i*i;
        
        
        
             for(i=0; i<3; i++) //current values
                printf("%d , ", array[i]);
             putchar('\n');
            // attempt to free 
            free(array);
        
            // print array elements
            for(i=0; i<3; i++) //values after exposure functions free()
                printf("%d , ", array[i]);
            putchar('\n');
        
        
            return 0;
        
        }
        

        【讨论】:

          【解决方案6】:
          #include <stdio.h>
          #include <stdlib.h>
          #include <string.h>
          
          int main()
          {
          
              // malloc the array
              // <-- allocated 12 bytes of memory in the heap
              // <-- set a pointer-to-int 'array' to point to that memory
              // <-- in C, should NOT cast returned value from malloc (and family of functions)
              int* array = (int*) malloc(sizeof(int)*3);
          
              // give some values
              // <-- set allocated memory in the heap 
              //     to 0x0000001 0x00000002 0x00000005
              int i;
              for(i=0; i<3; i++) array[i] = 1 + i*i;
          
              // attempt to free
              // <-- unallocate all 12 bytes
              free(array+0);
          
              // <-- corrupt the heap, this not an address pointer
              // <-- returned by malloc (or family of functions)
              free(array+1);
          
              // <-- corrupt the heap some more, this is not an address pointer
              // <-- returned by malloc (or family of functions)
              free(array+2);
          
              // print array elements
              // <-- trying to print using array as a base address ptr,
              // <-- when array no longer points to any allocated memory
              // <-- array still points into the heap, but the 
              // <-- 12 bytes are already returned to the OS
              // <-- so the program should not be accessing those 12 bytes
              for(i=0; i<3; i++) printf("%d , ", array[i]);
              return 0;
          } // end function: main
          

          【讨论】:

            猜你喜欢
            • 2018-09-16
            • 2014-12-20
            • 2015-09-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-02-10
            • 1970-01-01
            相关资源
            最近更新 更多