Dynamic Memory | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Dynamic Memory

#include <iostream> using namespace std; int main() { int *p = new int; // request memory *p = 5; // store value cout << *p << endl; // use value delete p; // free up the memory return 0; } please explain the step "request memory" and "storing value" and whats this "new int" is this some kind of address as it is put equivalent to a pointer ? please help

21st Jun 2017, 3:18 PM
Subhankar Golder
Subhankar Golder - avatar
1 Answer
+ 3
Request Memory - It is the process of allocating or reserving memory for use in the heap, for fast access. The compiler uses 'new' to get any empty memory spaces and reserve them for storing p's data. Doing this prevents the OS or Compiler to use this reserved memory for other processes like for local variables, etc. The compiler then assign an address (of any empty block) to this p for future references to this particular memory block and allow the program to store data in it. Storing Value - It is the process when we assign a value to the memory block at the assigned location so that it can be used later. We use * as dereference to pass the value into the block of memory at the particular address given to p during memory request.
21st Jun 2017, 3:22 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar