What is dynamic memory management for array?
Submitted by: AdministratorUsing 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 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;
}
Submitted by: Administrator
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 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;
}
Submitted by: Administrator
Read Online C++ New And Delete Job Interview Questions And Answers
Top C++ New And Delete Questions
☺ | Explain realloc()? |
☺ | Explain the difference between realloc() and free()? |
☺ | What is new operator and delete operator? |
☺ | What is dynamic memory management for array? |
☺ | You can use C++ as a procedural, as well as an object-oriented language? |
Top C Plus Plus Language Categories
☺ | C++ Pointers & Functions Interview Questions. |
☺ | C++ Operator Overloading Interview Questions. |
☺ | C++ Exception Handling Interview Questions. |
☺ | C++ Virtual Functions Interview Questions. |
☺ | C++ Template Interview Questions. |