gpt4 book ai didi

c - 如何释放我需要的结构指针

转载 作者:行者123 更新时间:2023-11-30 15:05:42 26 4
gpt4 key购买 nike

我试图避免代码中的内存泄漏。我需要取消分配 pElement、line 和 pSecond,而不丢失 pImage 内的值。因为我需要在打印函数中打印这些值。

我的add函数包含struct GraphicElement *pElements;、struct GraphicElement *pSecond;、struct Point point;。

我使用 malloc 为每个结构分配内存,然后添加值,然后将最终值传递到 pImage 中。除了我总是会出现 3 次内存泄漏这一事实之外,我的所有其他功能都运行良好。因为我没有 free(pSecond);....free(pElement)...free(line);

如果我尝试在函数退出之前以及将值传递到 pImage 之后释放它们。我的值(value)观都被抹去了。

如何在本地释放我的 add 函数中的这些值?

struct Point
{
int x, y;
};

struct Line
{
Point start;
Point end;
};

struct GraphicElement
{
enum{ SIZE = 256 };
unsigned int numLines; //number of lines
Line* pLines; //plines points to start and end
char name[SIZE];
};

typedef struct
{
unsigned int numGraphicElements;
GraphicElement* pElements; //the head points to pLines
} VectorGraphic;

void InitVectorGraphic(VectorGraphic*); //initializes pImage->pElement
void AddGraphicElement(VectorGraphic*); //Used to add
void ReportVectorGraphic(VectorGraphic*); // prints pImage contents
void CleanUpVectorGraphic(VectorGraphic*); //deallocates memory

最佳答案

How can I free those values inside my add function locally?

无法显式释放本地分配的内存。也不本地释放一些内存。一旦释放,内存插槽将无法访问,并且内部存储的数据将丢失。

在 C 中,您有两种选择来分配一些内存:您可以在堆上或在堆栈上分配它。堆上保留的内存槽可以全局访问,并且将一直保留到被显式释放为止。堆栈中保留的内容仅当您处于它们创建的上下文中时才有效。

假设您执行以下代码:

void func()
{
int x = 3; // [2]
int * p = & x; // [3]
}

int main()
{
func(); // [1]

// [4]
return 0;
}

指令[2]将在堆栈上分配一些内存。第二个 ([3]) 将执行相同的操作,并将第一个变量的地址存储在新的内存槽中。函数返回 ([4]) 后,该内存将被释放。从图形上看,发生的情况如下:

       Context   STACK    Address
+---------+
| | 0xa1
main | | 0xa0
+---------+

[1] +---------+
=====> | |
func | | 0xa2
+---------+
| | 0xa1
main | | 0xa0
+---------+

[2] +---------+
=====> | |
func | 3 | 0xa2 <-- x
+---------+
| | 0xa1
main | | 0xa0
+---------+

[3] +---------+
=====> | 0xa2 | 0xa3 <-- p
func | 3 | 0xa2 <-- x
+---------+
| | 0xa1
main | | 0xa0
+---------+

[4] +---------+
=====> | | 0xa1
main | | 0xa0
+---------+

So if i use malloc inside a function. Once I exist the function the allocated memory on the heap is freed automatically?

恰恰相反。如果您使用像malloc这样的函数,内存槽将在堆上分配。因此,如果我们将上面的 [3] 行更改为类似

int * p = malloc(sizeof(int));  // [3]

当您离开该函数时,堆栈上分配的内存将被释放,但堆上分配的内存将保持分配状态,并且仍然可以访问,直到您释放它为止。以图形方式:

                                                  HEAP     Address Free (y/n)
+---------+
| | 0xb4 - Yes
| | 0xb3 - Yes
+---------+
Context STACK Address
[3] +---------+ +---------+
=====> | 0xb4 | 0xa3 <-- p | | 0xb4 - No
func | 3 | 0xa2 <-- x | | 0xb3 - Yes
+---------+ +---------+
| | 0xa1
main | | 0xa0
+---------+

[4] +---------+ +---------+
=====> | | 0xa1 | | 0xb4 - No !!! Leak !!!
main | | 0xa0 | | 0xb3 - Yes
+---------+ +---------+

正如您所看到的,离开该函数后,您会出现内存泄漏,因为您没有任何指向动态分配内存的指针。避免这种情况的一种方法是返回指针(以便将新内存槽的地址传递给调用函数)或将其存储在某处以便稍后释放。也可以在调用函数之前分配内存并将其作为参数传递给函数。这实际上取决于您的应用程序。

关于c - 如何释放我需要的结构指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39689795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com