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

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

问题描述

我正在使用 DevStudio 在 Windows 上以非托管的 C/C++ 进行开发.

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天全站免登陆