Stack Vs Heap
Stack:
When you write simple programs in C and C++, you don't care where the memory for your variables are allocated, as by default they all ended up on Stack.
The stack is where memory is allocated for automatic variables within functions. A stack is a Last In First Out (LIFO) storage device where new storage is allocated and deallocated at only one ``end'', called the Top of the stack.
Example program;
In the program, shown above the scope of array myArray is limited to enclosing braces {}, once we return from myFunction, the scope of myArray ends, which will cleaned up and in the main function there is no way to access myArray anymore.
There are couple of problems in the program which used allocations on Stack.
First you can't access the memory once we reached the scope brace.
Second you have to know in advance how much memory will be needed, which is sometimes hard to predict.
Third there is a certain limit on how much can be allocated on Stack, memory allocations on Heap also suffer from this problem, but in the case of Stack the limit is too low.
The memory allocated on Stack is also called Static Memory Allocation.
Heap:
When a variable is defined in the source program, the type of the variable determines how much memory the compiler allocates. When the program executes, the variable consumes this amount of memory regardless of whether the program actually uses the memory allocated. This is particularly true for arrays. However, in many problems, it is not clear at the outset how much memory the program will actually need. Up to now, we have declared arrays to be ``large enough'' to hold the maximum number of elements we expect our application to handle. If too much memory is allocated and then not used, there is a waste of memory. If not enough memory is allocated, the program is not able to handle the input data.
We can make our program more flexible if, during execution, it could allocate additional memory when needed and free memory when not needed. Allocation of memory during execution is called dynamic memory allocation. C provides library functions to allocate and free memory dynamically during program execution. Dynamic memory is allocated on the heap by the system.
It is important to realize that dynamic memory allocation also has limits. If memory is repeatedly allocated, eventually the system will run out of memory.
Look at another example which uses memory allocated on heap.
There is one big problem using Heap, which is once you have allocated memory on Heap, now its your responsibility to free that memory once you are done with it.
In the program above we definitely leaked 4 bytes of memory. This is not a problem for small programs but once you started to write complicated programs then it can become a big problem.
The following program frees the memory once the user is done with the allocated memory.
C programmers use malloc to allocate memory and free to deallocate memory
C++ programmers use new to allocated memory and delete to deallocate memory.
Be careful malloc is incompatible with delete, same is true for new which is incompatible with free. Always use free to deallocate memory allocated using malloc.
Always use delete to deallocate memory allocated using new.
Hope some people will find this small tutorial useful.
http://ee.hawaii.edu/~tep/EE160/Book/chap1...ion2.1.1.8.html
Stack:
When you write simple programs in C and C++, you don't care where the memory for your variables are allocated, as by default they all ended up on Stack.
The stack is where memory is allocated for automatic variables within functions. A stack is a Last In First Out (LIFO) storage device where new storage is allocated and deallocated at only one ``end'', called the Top of the stack.
Example program;
Code:
#include <iostream.h>
using namespace std;
void myFunction()
{
int myArray[10] = {1,2,3,4,5,6,7,8,9,10};
for(int i = 0; i < 10; i++)
cout<<<<myArray[i]<<endl;
}
int main()
{
myFunction();
return 0;
}
There are couple of problems in the program which used allocations on Stack.
First you can't access the memory once we reached the scope brace.
Second you have to know in advance how much memory will be needed, which is sometimes hard to predict.
Third there is a certain limit on how much can be allocated on Stack, memory allocations on Heap also suffer from this problem, but in the case of Stack the limit is too low.
The memory allocated on Stack is also called Static Memory Allocation.
Heap:
When a variable is defined in the source program, the type of the variable determines how much memory the compiler allocates. When the program executes, the variable consumes this amount of memory regardless of whether the program actually uses the memory allocated. This is particularly true for arrays. However, in many problems, it is not clear at the outset how much memory the program will actually need. Up to now, we have declared arrays to be ``large enough'' to hold the maximum number of elements we expect our application to handle. If too much memory is allocated and then not used, there is a waste of memory. If not enough memory is allocated, the program is not able to handle the input data.
We can make our program more flexible if, during execution, it could allocate additional memory when needed and free memory when not needed. Allocation of memory during execution is called dynamic memory allocation. C provides library functions to allocate and free memory dynamically during program execution. Dynamic memory is allocated on the heap by the system.
It is important to realize that dynamic memory allocation also has limits. If memory is repeatedly allocated, eventually the system will run out of memory.
Look at another example which uses memory allocated on heap.
Code:
#include <iostream.h>
using namespace std;
int* myFunction()
{
int *myArray = malloc(10 * sizeof(int));
for(int i = 0; i < 10; i++)
cout<<<<myArray[i]<<endl;
return myArray;
}
int main()
{
int *array = myFunction();
//in this case the array allocated in myFunction is available here.
return 0;
}
In the program above we definitely leaked 4 bytes of memory. This is not a problem for small programs but once you started to write complicated programs then it can become a big problem.
The following program frees the memory once the user is done with the allocated memory.
Code:
#include <iostream.h>
using namespace std;
int* myFunction()
{
int *myArray = malloc(10 * sizeof(int));
for(int i = 0; i < 10; i++)
cout<<<<myArray[i]<<endl;
return myArray;
}
int main()
{
int *array = myFunction();
//in this case the array allocated in myFunction is available here.
free(array);
return 0;
}
C++ programmers use new to allocated memory and delete to deallocate memory.
Be careful malloc is incompatible with delete, same is true for new which is incompatible with free. Always use free to deallocate memory allocated using malloc.
Always use delete to deallocate memory allocated using new.
Hope some people will find this small tutorial useful.
http://ee.hawaii.edu/~tep/EE160/Book/chap1...ion2.1.1.8.html
Last edited: