当我尝试在 C++ 中使用“new”运算符分配大内存时,系统调用在此内存分配操作后不工作。问题似乎出在内存分配上。但是,我无法捕获异常。
为了更清楚起见,我有以下 MWE。
我的 main.cpp 包含以下几行:
#include <cstdio>
#include <iostream>
#include "memallocator.h"
#include <stdlib.h>
using namespace std;
const int MAX_ITER = 2000;
int main()
{
printf("\n 1. output of ls: %d \n", system("ls"));
for(int i=0;i<MAX_ITER;i++)
{
MemAllocator *pCMemAllocator;
try
{
pCMemAllocator = new MemAllocator(); // alocates memory of size BUF_SIZE = 4000*1024;
} catch(bad_alloc e)
{
printf("\nException at %d",i);
break;
}
}
printf("\nMemory Allocated");
printf("\n 2. output of ls: %d \n", system("ls"));
return 0;
}
memallocator.h 文件如下:
#ifndef MEMALLOCATOR_H
#define MEMALLOCATOR_H
const unsigned long BUF_SIZE = 4000*1024;
class MemAllocator
{
public:
MemAllocator(){};
private:
unsigned char ucBuffer [BUF_SIZE];
};
#endif // MEMALLOCATOR_H
我在 Ubuntu 14.04 64 位上使用 g++(版本 4.8)编译它并得到以下输出:
a.out main.cpp memallocator.h
1. output of ls: 0
Memory Allocated
2. output of ls: -1
即内存分配操作后system("ls") 命令失败。但是,异常处理程序未捕获失败。我还尝试了天真的方法:先将零分配给指针,然后在内存分配操作后检查零。该方法也没有捕捉到内存分配的问题。
如果我将变量 MAX_ITER 设置为某个较小的值,比如 20,一切正常。 对于更高的 MAX_ITER 值,内存异常应该由异常处理程序处理。为什么没有发生?
最佳答案
system(3)函数是(在 Linux 和大多数 Posix 系统上)使用 fork(2)系统调用,可能由于多种原因而失败
EAGAIN: fork() cannot allocate sufficient memory to copy the parent's page tables and allocate a task structure for the child.
ENOMEM: fork() failed to allocate the necessary kernel structures because memory is tight.
因此,如果您当前的进程使用大量内存(例如,如果系统没有足够的交换空间来保存拷贝),您将无法fork
。
顺便说一句,system(3)
手册页明确指出 system
可能会失败并返回....
* If a child process could not be created, or its status could not be retrieved, the return value is -1.
考虑使用 strace(1) - 可能作为 strace -f
了解您的应用程序完成的系统调用。
请注意 system(3)
不是 syscall (在 syscalls(2) 中列出)但是一个标准库函数。
所以你的巨大内存分配如你所愿,但后来对 system
的调用失败了(你应该测试它是否失败)。您的情况没有内存分配失败。
关于c++ - 无法捕获内存分配失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25483982/