在C语言中重用内存

在C语言中重用内存,c,pointers,memory,C,Pointers,Memory,我在使用和理解C中的free()函数时遇到问题 我试图编写这个示例以继续重用指针,但我不明白为什么会发生错误: #include <stdlib.h> #include <stdio.h> #include <string.h> typedef struct box { char message[20]; }box; box *newBox() { box *inBox; inBox = (box *) malloc(sizeo

我在使用和理解C中的free()函数时遇到问题

我试图编写这个示例以继续重用指针,但我不明白为什么会发生错误:

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


typedef struct box
{
    char message[20];
}box;


box *newBox()
{
    box *inBox;
    inBox = (box *) malloc(sizeof(box));

    return inBox;
}


int main()
{
    box *temp = NULL;


    temp = newBox();
    strcpy(temp->message,  "Hello");
    printf("%s\n", temp->message);

    free(temp);

    strcpy(temp->message,  "World");
    printf("%s\n", temp->message);

    free(temp);

    strcpy(temp->message,  "People");
    printf("%s\n", temp->message);

    return 0;   

}

有解决办法吗?

我不知道为什么这里有两个
免费(temp)
s:

temp = newBox();
strcpy(temp->message,  "Hello");
printf("%s\n", temp->message);

free(temp); /* ? */

strcpy(temp->message,  "World");
printf("%s\n", temp->message);

free(temp); /* ? */

strcpy(temp->message,  "People");
printf("%s\n", temp->message);
该代码应该是

temp = newBox();
strcpy(temp->message,  "Hello");
printf("%s\n", temp->message);

strcpy(temp->message,  "World");
printf("%s\n", temp->message);

strcpy(temp->message,  "People");
printf("%s\n", temp->message);

free(temp); /* Free the allocated memory after use */
一旦
释放了
内存,就不能使用它。因此,
最后释放
内存


旁注:演员阵容如下:

inBox = (box *) malloc(sizeof(box));
不需要,并且

执行此操作后,将释放
malloc()
(在本例中)分配的内存

因此,分配的内存已用完,无法再次使用,如果需要,必须重新分配内存


Else
free()
仅在对分配的内存执行所需操作后才释放内存。

一旦释放了通过malloc获得的内存,它就不再属于您,使用它是未定义的行为

每个malloc必须正好有一个空闲空间,并且在新malloc中使用指针之前必须先释放一个指针-但这没有多大意义,除非您进行测试或分配不同的内存大小(在最后一个用例中,您最好使用
realloc

假设您有充分的理由多次malloc/free(此代码不显示此要求),您可以:

int main()
{
    box *temp = NULL;


    temp = newBox();
    strcpy(temp->message,  "Hello");
    printf("%s\n", temp->message);

    free(temp);

    temp = newBox();
    strcpy(temp->message,  "World");
    printf("%s\n", temp->message);

    free(temp);

    temp = newBox();
    strcpy(temp->message,  "People");
    printf("%s\n", temp->message);
    free(temp);

    return 0;   

}
请不要在C中使用malloc结果!应该是:

box *newBox()
{
    box *inBox;
    inBox = malloc(sizeof(box));

    return inBox;
}

它还调用UB,因为同一内存被多次释放。哦,我以为每次需要将指针指向新对象时,都必须释放指针。@Cool Guy:+1表示删除强制转换。但是应该没有问题,因为OP包括@PravasiMeet Right。但由于链接中提到的其他原因,人们普遍不赞成这样做。但这并不重要,因为OP已经包含了
stdlib.h
。谢谢你告诉我@Pyrons规则很简单:对于通过
malloc
获得的每个指针,您需要对该指针执行一次
free
,一旦执行
free
操作,指针就不能再使用了。将“temp”传递给free()后,它就不再可用于程序中。在C中,不要强制转换malloc返回的值,因为它是一个void*,因此可以分配给任何其他指针。始终检查malloc返回的值(!=NULL),以确保操作成功。
int main()
{
    box *temp = NULL;


    temp = newBox();
    strcpy(temp->message,  "Hello");
    printf("%s\n", temp->message);

    free(temp);

    temp = newBox();
    strcpy(temp->message,  "World");
    printf("%s\n", temp->message);

    free(temp);

    temp = newBox();
    strcpy(temp->message,  "People");
    printf("%s\n", temp->message);
    free(temp);

    return 0;   

}
box *newBox()
{
    box *inBox;
    inBox = malloc(sizeof(box));

    return inBox;
}