COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button
C++ LANGUAGE DYNAMIC MEMORY ALLOCATION FOR ARRAY
Dynamic Memory Allocation for Arrays
Consider you want to allocate memory for an array of characters, i.e.,
string of 20 characters. Using the same syntax what we have used above
we can allocate memory dynamically as shown below.
char* pvalue = NULL; // Pointer initialized with null
pvalue = new char[20]; // Request memory for the variable
To remove the array that we have just created the statement would look like this:
delete [] pvalue; // Delete array pointed to by pvalue
Following the similar generic syntax of new operator, you can
allocate for a multi-dimensional array as follows:
double** pvalue = NULL; // Pointer initialized with null
pvalue = new double [3][4]; // Allocate memory for a 3x4 array
However, the syntax to release the memory for multi-dimensional
array will still remain same as above:
delete [] pvalue; // Delete array pointed to by pvalue
No comments:
Post a Comment