人为限制 C/C++ 内存使用
有什么方法可以轻松地将 C/C++ 应用程序限制为指定的内存量(30 mb 左右)?例如:如果我的应用程序尝试将 50mb 文件完全加载到内存中,它将死/打印一条消息并退出/等.
Is there any way to easily limit a C/C++ application to a specified amount of memory (30 mb or so)? Eg: if my application tries to complete load a 50mb file into memory it will die/print a message and quit/etc.
诚然,我可以不断地检查应用程序的内存使用情况,但如果我继续检查它会因错误而死亡,那会容易一些.
Admittedly I can just constantly check the memory usage for the application, but it would be a bit easier if it would just die with an error if I went above.
有什么想法吗?
平台不是大问题,windows/linux/任何编译器.
Platform isn't a big issue, windows/linux/whatever compiler.
阅读 unix 系统上 ulimit
的手册页.您可以在启动可执行文件或(在手册的第 3 节中)调用同名 API 之前调用内置的 shell.
Read the manual page for ulimit
on unix systems. There is a shell builtin you can invoke before launching your executable or (in section 3 of the manual) an API call of the same name.