简体   繁体   English

如何释放重新分配的内存? C ++

[英]How to free reallocated memory? C++

I'm trying to free memory that's reallocated but I get an error... 我正在尝试释放重新分配的内存但我收到错误...

float * foo = NULL;
float * bar = NULL;

void update()
{
    ...
    foo = (float *)malloc( a * 2 * sizeof(float));
    ...
    bar = (float *)realloc( foo, a * 2 * sizeof(float));
    ...
    free( foo );
    ...
    // when i do
    if(bar != NULL)
    {
        free(bar); // <-- error at executing
    }
}

I get error: http://d.pr/mpBF and visual studio shows me the following file: 我收到错误: http//d.pr/mpBF和visual studio向我显示以下文件:

osfinfo.c
=========
void __cdecl _unlock_fhandle (
        int fh
        )
{
        LeaveCriticalSection( &(_pioinfo(fh)->lock) );
}

Any ideas? 有任何想法吗?

foo = (float *)malloc( a * 2 * sizeof(float));
bar = (float *)realloc( foo, a * 2 * sizeof(float));
free( foo ); // oops, foo has gone

At the point at which you call free(foo) , foo is invalid since it was already freed when you called realloc . 在你调用free(foo)foo是无效的,因为当你调用realloc时它已经被释放了。

The code should be something like this pseudo-code: 代码应该像这样的伪代码:

foo = (float *)malloc( a * 2 * sizeof(float));
if (foo == NULL) 
    return ERROR_CODE;
...
bar = (float *)realloc( foo, a * 2 * sizeof(float));
if (bar == NULL) 
{
    free(foo);
    return ERROR_CODE;
}
...
free(bar);
return SUCCESS;

Of course, since this is C++, you should be avoiding malloc and free altogether and using std::vector<float> . 当然,因为这是C ++,你应该避免使用malloc并完全free并使用std::vector<float>

My guess is that the realloc call manages to extend the memory allocated by the call to malloc . 我的猜测是realloc调用设法扩展调用malloc分配的malloc In this case both foo and bar will point to the same memory address, and you're free ing foo somewhere before trying to free bar resulting in a double deletion. 在这种情况下, foobar都将指向相同的内存地址,并且在尝试free bar之前,您可以在某处free foo ,从而导致双重删除。

You do not need to free( foo ) at all because realloc will do that for you if the memory area was moved during reallocation. 您根本不需要free( foo )因为如果在重新分配期间移动了内存区域, realloc将为您执行此操作。 From the linked page: 从链接页面:

If the area pointed to was moved, a free(ptr) is done. 如果指向的区域被移动,则完成自由(ptr)。

When you realloc memory you should not free the old memory. 重新分配内存时,不应释放旧内存。

bar = (float *)realloc( foo, a * 2 * sizeof(float));
free( foo ); // <-- this is wrong

You want: 你要:

float * foo = NULL;

void update()
{
    ...
    foo = (float *)malloc( a * 2 * sizeof(float));
    ...
    float * bar = (float *)realloc( foo, a * 2 * sizeof(float));
    if(bar)
       foo = bar;
    ...
    free(foo);
}

Once you've passed a pointer to realloc() , it is formally deallocated (freed) and you must not free it again. 一旦你传递了一个指向realloc()的指针,它就会被正式释放(释放),你不能再释放它。

C99 §7.20.3.4 The realloc function C99§7.20.3.4重新分配功能

2 The realloc function deallocates the old object pointed to by ptr and returns a pointer to a new object that has the size specified by size. 2 realloc函数释放ptr指向的旧对象,并返回指向具有size指定大小的新对象的指针。 The contents of the new object shall be the same as that of the old object prior to deallocation, up to the lesser of the new and old sizes. 新对象的内容应与解除分配之前的旧对象的内容相同,直到新旧大小中的较小者为止。 Any bytes in the new object beyond the size of the old object have indeterminate values. 新对象中超出旧对象大小的任何字节都具有不确定的值。

It may happen that realloc() returns the same pointer as it was given, but you cannot assume that it will in general. 可能会发生realloc()返回与给定的指针相同的指针,但您不能认为它通常会发生。 And once you've passed a pointer to realloc() (or free() ) you must assume it is no longer a valid pointer. 一旦你传递了一个指向realloc() (或free() )的指针,你必须假设它不再是一个有效的指针。

The rules in C++ are basically the same; C ++中的规则基本相同; it incorporates the C89 standard for functions from C such as realloc() . 它结合了C语言的C89标准,如realloc()

Your system is correct to complain that you're freeing unallocated memory. 你的系统抱怨你正在释放未分配的内存是正确的。

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

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