#include<iostream>
int y[10];
void doSomething()
{
int x[10];
int *z = new int[10];
//Do something interesting
delete []z;
}
int main()
{
doSomething();
}
int x[10];
– Creates an array of size 10 integers on stack.
– You do not have to explicitly delete this memory because it goes away as stack unwinds.
– Its scope is limited to the function doSomething()
int y[10];
– Creates an array of size 10 integers on BSS/Data segment.
– You do not have to explicitly delete this memory.
– Since it is declared global
it is accessible globally.
int *z = new int[10];
– Allocates a dynamic array of size 10 integers on heap and returns the address of this memory to z
.
– You have to explicitly delete this dynamic memory after using it. using:
delete[] z;