0

What is Dynamic memory allocation?

7th Dec 2016, 7:54 AM
Nikhil Sharma
Nikhil Sharma - avatar
1 Answer
0
There are two ways that memory gets allocated for data storage: Compile Time (or static) Allocation Memory for named variables is allocated by the compilerExact size and type of storage must be known at compile timeFor standard array declarations, this is why the size has to be constant. Dynamic Memory Allocation Memory allocated "on the fly" during run timedynamically allocated space usually placed in a program segment known as the heap or the free storeExact amount of space or number of items does not have to be known by the compiler in advance.For dynamic memory allocation, pointers are crucial. To allocate space dynamically, use the unary operator new, followed by the type being allocated. new int; // dynamically allocates an int new double; // dynamically allocates a double If creating an array dynamically, use the same form, but put brackets with a size after the type: new int[40]; // dynamically allocates an array of 40 ints new double[size]; // dynamically allocates an array of size doubles Note that the size can be a variable.
7th Dec 2016, 9:18 AM
Vipul Walia
Vipul Walia - avatar