C++ program to fill whole memory

Hello learners, today in this tutorial we will learn how to write a program in C++ that can fill the whole memory of our system. With the help of the new keyword, we can allocate the memory and With the help of the delete keyword, we can deallocate the memory. Let’s discuss this in more detail.

Program to fill whole memory

Approach: If we continuously allocate the memory using dynamic memory allocation or with the help of the new keyword. It will eventually fill the whole memory.

Syntax of the new operator

To allocate the memory dynamically

pointer_variable = new data_type;

Syntax of the delete operator

To deallocate the memory

delete pointer_variable;


CAUTION: 

DON’T TRY THIS IN YOUR SYSTEM IT WILL HANG YOUR SYSTEM WITHIN FEW SECONDS. THE SYSTEM WILL NOT WORK PROPERLY BECAUSE OF THE SHORTAGE OF MEMORY SPACE.

Here is the code:

#include<iostream>
using namespace std;

int main(){
    
    int *p;
    
    while (1) {
        
//      To dynamically allocate the memory
        p = new int[10];
        
//      To deallocate the memory
//        delete [] p;
    }
    
    return 0;
}

Output:

We don't get any output to display. But you can check about the memory status in your Task Manager to get the idea about the memory or output.

 

Note: Always use the delete keyword to free the memory space whenever you will use the new keyword to allocate the memory dynamically.

 

I hope this article was helpful to you. Keep Coding Keep Learning.

Also Read:

  1. Dynamic memory allocation in C++
  2. std::to_address in C++ with example

Leave a Reply

Your email address will not be published. Required fields are marked *