能够填满整个内存的 C++ 程序


在此文章中,我们将探讨如何编写一个简单的 C++ 程序来填满整个内存。此处的逻辑非常简单。我们将使用动态内存分配创建新的整数变量。如果我们反复创建一些变量,它将填满整个主内存。

在 C++ 中,我们可以使用新关键字动态分配内存空间。

new 操作符的基本语法如下所示。

pointer_var = new data_type

若要取消分配内存空间,我们可以使用 delete 关键字。语法为

delete pointer_var

注意运行此程序后,可能会减慢系统的性能。由于内存空间不足,整个系统可能无法正常运行。

示例代码

#include<iostream>
using namespace std;
main() {
   while(true) {
      int *var = new int; //allocate memory dynamically
   }
}

输出

Here we cannot get any specific output to display. We can check the
memory status in the task manager to get the idea about the output.

更新于: 30-Jul-2019

254 浏览量

启动您的 职业

通过完成课程获得认证

开始
广告
© . All rights reserved.