C++ (memory allocation to an array)

1) int *a=new int[10]
2) int a[10]
can anybody tell the exact differences in these two types of methods of allocating memory for an array ? When does 1st method is useful and when does 2nd ?I also read somewhere that in Ist method memory is allocated from heap but i don't know from where memory is allocated in 2nd method and what difference these memory allocations causes.
The first one is referred to as "dynamic allocation" which means the memory is allocated at run-time. The second one is just the declaration of the array's size and so the size of the array has to be a constant value.
The first gets allocated on the heap and the second one on the stack.
When creating a variable on the stack, it will get deleted automatically once it leaves its scope.
But when you create something with "new" you have to take care of deletion yourself, if you don't then the memory allocated will stay allocated until the end of your program, allocating memory and forgetting to free it is called a memory leak.
The stack is a relatively small fixed size of memory, the heap is dynamic and can change, depending on how much you need, as long as you have enough RAM.
Because the stack is small you shouldnt create huge objects on the stack, for instance large arrays.
Just try it out it should give you a stack overflow error:
int a[100000000];
To allocate a big array like that you need to do it on the heap, that is done using the "new" keyword.
But make sure to delete it again using delete[] when you're done.
Creating objects on the heap happens during runtime, while for the stack the compiler will usually look through your code to see how big the stack needs to be.
For instance if it sees you have 2 variables of type int, it knows it needs to allocate 8 bytes for that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int a = 1; // Created on the stack
int b[3] = {1,2,3}; // Also created on the stack
int* c = new int(1); // Created '1' on the heap
int* d = nullptr; // d is just a pointer to an int, the int has not been created yet, only the variable to hold its adress has been created, on the stack
{
	int e = 1; // in stack, is only valid inside this scope {} and after it leaves its scope it will get deleted
	int f[3] = { 1, 2, 3 };
	d = new int[3]; // Here we create an int array of size 3 on the heap, new[] returns the adress of the first int, so we assign that adress to d
	d[0] = 1;
	d[1] = 2;
	d[2] = 3;
}
// Here e and f got deleted, but d still exists in heap memory, because we created it in the scope above but didn't call delete on it.
// It will exist until we delete it, no matter where we created it. Don't forget to do delete it using delete[] because it got created using new[] instead of new, making it an array.
delete[] d;
// After d has been deleted and you try to access it again you will either get an error or some random data.
// Better set it to 'nullptr' to signify that it has been deleted and doesn't point to anything valid anymore.
d = nullptr;
// Here a still exists, b still exists, e and f don't and d doesn't because we deleted it
// Don't forget do delete c because we created it on the heap with 'new'
delete c;
c = nullptr;


When you don't know how big your array needs to be, and is determined by a variable, like so:
1
2
int arraySize = randomNumber();
int arr[arraySize];

It's not possible to do that because the compiler needs to know how much memory it has to allocate for the stack beforehand.
But because arraySize gets calculated in runtime and not compiletime, the size is unknown to the compiler.
So you have to create it on the heap:
1
2
int arraySize = randomNumber();
int* arr = new int[arraySize];


It's really hard to understand it all at once, just take it slow and experiment with a simple program.
I hope I didn't make any mistakes, I'm not really sure myself, it confused me too just now while I was trying to explain it :)
thanks @Horscht and @Computergeek01.Also @Horscht example you gave is really helpful :)
Topic archived. No new replies allowed.