- The stack memory is when a function is called, the memory is allocated for local variables etc and is released when out of scope.
- Memory is allocated on the heap when the new keyword is used and must be released by using the delete keyword.
int foo()
{
char *pBuffer; //<--nothing allocated yet (excluding the pointer itself, which is allocated here on the stack).
bool b = true; // Allocated on the stack.
if(b)
{
//Create 500 bytes on the stack
char buffer[500];
//Create 500 bytes on the heap
pBuffer = new char[500];
}//<-- buffer is deallocated here, pBuffer is not
}//<--- oops there's a memory leak, I should have called delete[] pBuffer;
[source]
[source]
No comments:
Post a Comment
Note: only a member of this blog may post a comment.