Professional Documents
Culture Documents
int main()
{
int* foo;
int main()
{
int* foo;
foo = new int;
int main()
{
int* foo;
foo = new int;
*foo = 3;
int main()
{
int* foo;
foo = new int;
*foo = 3;
foo = new int;
//More code goes here
}
Dynamic Memory Allocation
The Stack
foo
using namespace std;
int main()
{
int* foo;
foo = new int;
*foo = 3;
foo = new int; The Heap
//More code goes here
}
Dynamic Memory Allocation
The Stack
foo
using namespace std;
int main()
{
int* foo;
foo = new int;
*foo = 3;
foo = new int; The Heap
//More code goes here
} int
Dynamic Memory Allocation
The Stack
foo
using namespace std;
int main()
{
int* foo;
foo = new int;
*foo = 3;
foo = new int; The Heap
//More code goes here
} int
3
Dynamic Memory Allocation
The Stack
foo
using namespace std;
int main()
{
int* foo;
foo = new int;
*foo = 3;
foo = new int; The Heap
//More code goes here
} int int
3
Dynamic Memory Allocation
The Stack
foo
using namespace std;
int main()
{
int* foo;
foo = new int;
*foo = 3;
foo = new int; The Heap
//More code goes here
} int int
3
We have
ABANDONED this
integer!
int
3
We have
ABANDONED this
integer!
int
We have NO WAY to
3
tell the computer to
free up the memory
it resides in!
WE HAVE
LEAKED
THIS
MEMORY
In this example, the amount of memory we
leaked was pretty small (only a few bytes, really)
In this example, the amount of memory we
leaked was pretty small (only a few bytes, really)
foo
using namespace std;
int main()
{
int* foo;
foo = new int;
*foo = 3;
The Heap
foo = new int;
//More code goes here int
} 3
How to Use the delete Operator
The Stack
foo
using namespace std;
int main()
{
int* foo;
foo = new int;
*foo = 3;
delete foo; The Heap
foo = new int;
//More code goes here
}
How to Use the delete Operator
The Stack
foo
using namespace std;
int main()
{
int* foo;
foo = new int;
*foo = 3;
delete foo; The Heap
foo = new int;
//More code goes here int
}
How to Use the delete Operator
The Stack
foo
using namespace std;
int main()
{
int* foo;
foo = new int;
*foo = 3;
delete foo; The Heap
foo = new int;
//More code goes here int
}