调用_freea真的有必要吗? [英] Call to _freea really necessary?

查看:234
本文介绍了调用_freea真的有必要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用不受管理的C/C ++在带有DevStudio的Windows上进行开发.

I am developping on Windows with DevStudio, in C/C++ unmanaged.

我想在堆栈而不是堆上分配一些内存,因为我不想手动处理释放该内存(我了解智能指针以及所有这些东西.我有一个非常特殊的内存情况分配,我需要处理),类似于A2W()和W2A()宏的使用.

I want to allocate some memory on the stack instead of the heap because I don't want to have to deal with releasing that memory manually (I know about smart pointers and all those things. I have a very specific case of memory allocation I need to deal with), similar to the use of A2W() and W2A() macros.

_alloca 可以这样做,但已弃用.建议使用 malloca .但是_malloca文档说,每次调用_malloca都必须调用___ freea .然后,这违反了我使用_malloca的目的,我将使用malloc或new代替.

_alloca does that, but it is deprecated. It is suggested to use malloca instead. But _malloca documentation says that a call to ___freea is mandatory for each call to _malloca. It then defeats my purpose to use _malloca, I will use malloc or new instead.

任何人都知道我是否可以不泄漏而不会调用_freea以及可以对内部产生什么影响?

Anybody knows if I can get away with not calling _freea without leaking and what the impacts are internally?

否则,我将只使用不推荐使用的_alloca函数.

Otherwise, I will end-up just using deprecated _alloca function.

推荐答案

每次调用_malloca之后都调用_freea总是很重要.

It is always important to call _freea after every call to _malloca.

_malloca类似于_alloca,但是增加了一些额外的安全检查和增强功能以​​保护您.结果,_malloca可以在堆而不是堆栈上进行分配.如果发生这种情况,并且您不调用_freea,则会发生内存泄漏.

_malloca is like _alloca, but adds some extra security checks and enhancements for your protection. As a result, it's possible for _malloca to allocate on the heap instead of the stack. If this happens, and you do not call _freea, you will get a memory leak.

在调试模式下,_malloca总是在堆上分配,因此也应释放.

In debug mode, _malloca ALWAYS allocates on the heap, so also should be freed.

搜索_ALLOCA_S_THRESHOLD以了解有关阈值如何工作以及_malloca代替_alloca的原因的详细信息,

Search for _ALLOCA_S_THRESHOLD for details on how the thresholds work, and why _malloca exists instead of _alloca, and it should make sense.

有评论表明该人只是在堆上分配,并使用智能指针等.

There have been comments suggesting that the person just allocate on the heap, and use smart pointers, etc.

堆栈分配有很多优点,_malloca将为您提供堆栈分配,因此有很多理由希望这样做. _alloca将以相同的方式工作,但是很可能导致堆栈溢出或其他问题,不幸的是,它没有提供很好的异常,而是倾向于破坏您的进程. _malloca在这方面更加安全,并且可以保护您,但是代价是您仍然需要使用_freea释放内存,因为_malloca可能(但不太可能在释放模式下)选择在堆上而不是堆栈上进行分配.

There are advantages to stack allocations, which _malloca will provide you, so there are reasons for wanting to do this. _alloca will work the same way, but is much more likely to cause a stack overflow or other problem, and unfortunately does not provide nice exceptions, but rather tends to just tear down your process. _malloca is much safer in this regard, and protects you, but the cost is that you still need to free your memory with _freea since it's possible (but unlikely in release mode) that _malloca will choose to allocate on the heap instead of the stack.

如果您唯一的目标是避免释放内存,那么我建议您使用一个智能指针,该指针将在成员超出范围时为您处理内存的释放.这样可以在堆上分配内存,但是很安全,并且可以避免释放内存.不过,这仅在C ++中有效-如果您使用的是普通C语言,则此方法将无效.

If your only goal is to avoid having to free memory, I would recommend using a smart pointer that will handle the freeing of memory for you as the member goes out of scope. This would assign memory on the heap, but be safe, and prevent you from having to free the memory. This will only work in C++, though - if you're using plain ol' C, this approach will not work.

如果您出于其他原因(通常是性能,因为堆栈分配非常非常快)而试图在堆栈上进行分配,我建议您使用_malloca并考虑到需要在值上调用_freea的事实

If you are trying to allocate on the stack for other reasons (typically performance, since stack allocations are very, very fast), I would recommend using _malloca and living with the fact that you'll need to call _freea on your values.

这篇关于调用_freea真的有必要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆