简体   繁体   English

释放内存问题

[英]freeing memory issue

I have an issue regarding freeing memory as under: 我有一个关于释放内存的问题,如下:

            string points; // some points sequences

    char* charPoints = (char*)malloc((points.length() +1) * sizeof(char));

            //do something

    free(charPoints);

Even then after freeing memory is leaked when checked with instruments 即使这样,在用仪器检查后释放内存也会泄漏

The only problem I see is when malloc returns NULL because it can't allocate enough contiguous memory. 我看到的唯一问题是当malloc返回NULL时,因为它无法分配足够的连续内存。 Are you sure that the memory leak is due to the malloc/free? 你确定内存泄漏是由malloc / free引起的吗?

The pointer you pass to free must be the same one which returned by malloc . 传递给free的指针必须与malloc返回的指针相同。 If you pass a different pointer it will result in a undefined behavior. 如果传递不同的指针,将导致未定义的行为。 Take a copy of the pointer before you do the operation such as incrementing the charPoints and then pass this original pointer to free function to properly release memory. 在执行操作之前获取指针的副本,例如递增charPoints ,然后将此原始指针传递给free函数以正确释放内存。

您的泄漏检测仪器很糟糕。

After freeing the memory update with NULL value shown bellow 释放内存更新后,显示如下所示的NULL值

free(charPoints); 自由(charPoints); charPoints ="NULL" charPoints =“NULL”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM