C释放阵列isn';行不通

C释放阵列isn';行不通,c,arrays,malloc,free,C,Arrays,Malloc,Free,这是我的密码。我正在创建一个数组,给元素一些值,然后释放和打印 #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

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

#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;
}
#包括
#包括
#包括
int main()
{
//malloc数组
int*数组=(int*)malloc(sizeof(int)*3);
//给出一些值
int i;

对于(i=0;i,不能逐个释放数组中的元素


你必须传递与
malloc
给你的
free
指针完全相同的指针。

C在幕后跟踪分配的内存。如果你想释放拥有malloced的内存块,你需要传递指向该内存块开头的指针。你应该使用:

free(array);
另外,您不应该强制执行malloc。

free(array+0)
已解除分配整个
sizeof(int)*3
数组,以下两个
free()
调用将导致未定义的行为(请参阅)

无空隙(空隙*ptr)

释放内存块

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

如果ptr未指向使用上述命令分配的内存块 函数,它会导致未定义的行为

如果ptr是空指针,则函数不执行任何操作


另外,请注意,在内存区域空闲后访问它会导致未定义的行为

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

显示正确的代码:

#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;

}
#包括
int main()
{
//malloc数组
int*数组=(int*)malloc(sizeof(int)*3);
//给出一些值
int i;
对于(i=0;i
#包括
#包括
#包括
int main()
{
//malloc数组

//不要强制执行
malloc()
。谢谢大家。仍然会得到相同的结果。@FeloVilches在释放后访问
array
,这是未定义的行为。也就是说,你的值在此之后可能存在,也可能不存在。@zenith好的,那么,即使打印了一些原始值,我是否应该相信数组已被释放?(我根据您的建议更改了代码)@FeloVilches是。
free
不会以任何方式覆盖已解除分配的数据。正如zenith所指出的,这是错误的,但即使不是,取消引用
free
d指针听起来像是未定义的行为one
free()
per
malloc()
,您释放的内存比预期的要多出三倍。malloc/free处理内存区域,它们不知道您如何构造这些区域。malloc需要知道区域的大小,但在实际调用该表达式之前会对其进行计算(生成一个数字)。如果执行一个malloc,则只需执行一个free(使用从malloc收到的指针作为参数)。
#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