What is Dynamic memory management for array?

What is dynamic memory management for array?

- Using the new and delete operators, we can create arrays at runtime by dynamic memory allocation.
- The general form for doing this is :
p_var = new array_type[size];
size specifies the no of elements in the array
To free an array we use:
delete[ ]p_var;   // the [ ] tells delete that an array is being freed.
- Consider the following program :
#include <iostream>
#include <new>
using namespace std;
int main()
{
   int *p, i;
   try
   {
       p = new int(10); //allocate array of 10 integers
   }
   catch (bad_alloc x)
   {
       cout << “Memory allocation failed”;
       return 1;
   }
   for (i = 0; i < 10; i++)
       p[i] = i;
   for (i = 0; i < 10; i++)
       cout <<p[i]<<”\n”;
   delete [ ] p; //free the array
   return 0;
}
What is C-string? - C++
What is C-string? - C-string is null terminated string which is simply a null terminated character array......
Problem with C-string - C++
Problem with C-string - C-string can not contain 0 as a character in them. C-strings are essentially null terminated strings.....
Difference between struct and class in terms of Access Modifier
Difference between struct and class - All members of a class are private by default, whereas fields of a struct are public.....
Post your comment