[英]What happens if my app is out of memory?
If my application is out of memory when i call new() i will get exception and malloc() i will get 0 pointer. 如果我的应用程序内存不足,当我调用new()时,我将获得异常,而malloc()将获得0指针。
But what if i call a method with some local variables? 但是,如果我调用带有局部变量的方法怎么办? They occupy memory too.
它们也占用内存。 Is there some way to reserve memory for "normal" variables?
有什么方法可以为“普通”变量保留内存吗? So that even though new() throws exception i can just catch it, fix stuff and still call methods like usual.
这样即使new()抛出异常,我也可以捕获它,修复问题并像往常一样调用方法。
Your data is allocated in one of three ways: 您的数据通过以下三种方式之一分配:
You generally shouldn't run out of stack space unless you perform some heavy recursion though. 但是,除非执行一些繁重的递归操作,否则通常不应该用完堆栈空间。
THe C++ language doesn't provide any mechanism for reserving memory for local variables. C ++语言不提供任何用于为局部变量保留内存的机制。 Your specific C++ implementation and/or operating system may provide some means of increasing the total stack size, but this is not normally necessary.
您的特定C ++实现和/或操作系统可以提供一些增加总堆栈大小的方法,但这通常不是必需的。
Note also that if a call to new does fail, there is probably very little you can practically do to recover from it. 还要注意,如果对new的调用确实失败了,从恢复中恢复实际上几乎没有什么可做的。 Many people (me included) no longer bother checking for new failure.
许多人(包括我在内)不再费心检查新的故障。
New allocates memory from the heap, but local vars are normally on the stack - which can overflow, but is less likely, depending on your platform. New从堆中分配内存,但是本地var通常在堆栈上-可能会溢出,但可能性较小,具体取决于您的平台。 Can you provide more details?
您能否提供更多详细信息?
The compiler knows how much of memory per stack you need. 编译器知道每个堆栈需要多少内存。 However, sufficiently high number of stacks (caused due to recursion) will crash your program -- there probably isn't another way to fix this.
但是,足够多的堆栈(由于递归导致)将使程序崩溃—可能没有其他方法可以解决此问题。
The standard has an interesting annexure called Implementation Quantities . 该标准有一个有趣的附件,称为“ 实施数量” 。 This is non-normative (informative) and hence should not be treated as the absolute truth, but provides you with a fair idea.
这是非规范性的(信息性的),因此不应将其视为绝对真理,而应为您提供一个公正的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.