NOTE:We strongly recommend to try this code in virtual machine because it may hang your computer within 5 second
Dynamic memory allocation in C/C++ refers to performing memory allocation manually by programmer. Dynamically allocated memory is allocated on Heap and non-static and local variables get memory allocated on Stack
The new operator denotes a request for memory allocation on the Heap. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable.
Syntax to use new operator: To allocate memory of any data type, the syntax is
pointer-variable = new data-type;
delete that perform the task of allocating and freeing the memory in a better and easier way.
Syntax to use delete operator: To allocate memory of any data type, the syntax is
Following is C++ Program that will fill whole memory
Output & Explanation
It hung my computer while the task manager was open and showed that it took 890 Mb of memory in 1 second then it also hung. It keeps on giving memory to a variable.To explore more of this code, added a statement delete a and every thing was fine while testing (no hanging) So, I think that the chunk of memory is given (due to new int) and then taken back (due to delete a) to the free space in the new code below.
This article is contributed by Akash Sharan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Program for Sum of the digits of a given number
- Quine - A self-reproducing program
- Program to compute Log n
- Output of C++ Program | Set 10
- Output of C++ Program | Set 14
- Output of C++ Program | Set 16
- Output of C++ Program | Set 18
- Program to print last 10 lines
- Program to validate an IP address
- Write a C program that does not terminate when Ctrl+C is pressed
- Write a C program that won't compile in C++
- Write a program that produces different results in C and C++
- OpenCV C++ Program to play a video
- fill() and fill_n() functions in C++ STL
- OpenCV C++ Program to blur a Video
- OpenCV C++ Program to create a single colored blank image
- OpenCV C++ Program to blur an image
- OpenCV C++ Program for coin detection
- C/C++ program to shutdown a system
- A creative C++ program to Zoom digits of an integer