dynamic memory allocation exception handling
TRANSCRIPT
-
8/13/2019 Dynamic Memory Allocation Exception Handling
1/37
2011 BlueSignet LLC. All rights reserved.
Programming inC++Dynamic Memory Allocation & Exception
Handling
-
8/13/2019 Dynamic Memory Allocation Exception Handling
2/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Dynamic Memory Allocation
Works in a very similar way to calloc that we used in C
Created with
The operator new
Similar to malloc/calloc
Destroyed with The operator delete
Similar to free
-
8/13/2019 Dynamic Memory Allocation Exception Handling
3/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Dynamic Memory Allocation
#include
#include
using namespace std;
int main(int argc, const char* argv[])
{
int SIZE = atoi(argv[1]);
int *integerArray = new int[SIZE];
for(int i = 0; i < SIZE; i++)
integerArray[i] = i;
for(int i = 0; i < SIZE; i++)
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
4/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Dynamic Memory Allocation
#include
#include
using namespace std;
int main(int argc, const char* argv[])
{
int SIZE = atoi(argv[1]);
int *integerArray = new int[SIZE];
for(int i = 0; i < SIZE; i++)
integerArray[i] = i;
for(int i = 0; i < SIZE; i++)
cout dynmem.exe 3
integerArray
-
8/13/2019 Dynamic Memory Allocation Exception Handling
5/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Dynamic Memory Allocation
#include
#include
using namespace std;
int main(int argc, const char* argv[])
{
int SIZE = atoi(argv[1]);
int *integerArray = new int[SIZE];
for(int i = 0; i < SIZE; i++)
integerArray[i] = i;
for(int i = 0; i < SIZE; i++)
cout dynmem.exe 3
-
8/13/2019 Dynamic Memory Allocation Exception Handling
6/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Dynamic Memory Allocation
#include
#include
using namespace std;
int main(int argc, const char* argv[])
{
int SIZE = atoi(argv[1]);
int *integerArray = new int[SIZE];
for(int i = 0; i < SIZE; i++)
integerArray[i] = i;
for(int i = 0; i < SIZE; i++)
cout dynmem.exe 30
1
2
-
8/13/2019 Dynamic Memory Allocation Exception Handling
7/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Dynamic Memory Allocation
#include
#include
using namespace std;
int main(int argc, const char* argv[])
{
int SIZE = atoi(argv[1]);
int *integerArray = new int[SIZE];
for(int i = 0; i < SIZE; i++)
integerArray[i] = i;
for(int i = 0; i < SIZE; i++)
cout dynmem.exe 30
1
2
-
8/13/2019 Dynamic Memory Allocation Exception Handling
8/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Dynamic Memory Allocation
#include
#include
using namespace std;
int main(int argc, const char* argv[])
{
int SIZE = atoi(argv[1]);
int *integerArray = new int[SIZE];
for(int i = 0; i < SIZE; i++)
integerArray[i] = i;
for(int i = 0; i < SIZE; i++)
cout dynmem.exe 30
1
2
-
8/13/2019 Dynamic Memory Allocation Exception Handling
9/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Dynamic Memory Allocation
#include
using namespace std;
int main()
{
string *str = new string;
*str = "This is my string";
delete str;
return 0;
}
-
8/13/2019 Dynamic Memory Allocation Exception Handling
10/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Dynamic Memory Allocation
#include
using namespace std;
int main()
{
string *str = new string;
*str = "This is my string";
delete str;
return 0;
}
str
-
8/13/2019 Dynamic Memory Allocation Exception Handling
11/37
-
8/13/2019 Dynamic Memory Allocation Exception Handling
12/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Dynamic Memory Allocation
#include
using namespace std;
int main()
{
string *str = new string;
*str = "This is my string";
delete str;
return 0;
}
01001
11011
str
-
8/13/2019 Dynamic Memory Allocation Exception Handling
13/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Dynamic Memory Allocation
#include
using namespace std;
int main()
{
string *str = new string;
*str = "This is my string";
delete str;
return 0;
}
str
-
8/13/2019 Dynamic Memory Allocation Exception Handling
14/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Dynamic Memory Allocation
#include
using namespace std;
int main()
{
string *str = new string("This is my string");delete str;
return 0;
}
-
8/13/2019 Dynamic Memory Allocation Exception Handling
15/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Dynamic Memory Allocation
Although new and delete act like keywords, they are
actually operators
When new is called, memory is allocated from a memorypool called the Free Store
malloc and calloc use the Heap
-
8/13/2019 Dynamic Memory Allocation Exception Handling
16/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Dynamic Memory Allocation
Free Store Heap
Allocates with new Uses malloc / calloc
Deallocates with delete Uses free
Type Identification returns
a pointer with designatedtype
Returns a void pointer
Automatically handlesallocation size
Requires the block size tobe passed
Can be overwritten withmethod overridding
malloc / alloc / freemethods are all static
-
8/13/2019 Dynamic Memory Allocation Exception Handling
17/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Dynamic Memory Allocation
Generally, it is generally good practice to use heap forprimitive data types and Free Space for objects
-
8/13/2019 Dynamic Memory Allocation Exception Handling
18/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
This allows us to attempt potentially dangerous Plan Acode, and if anything fails we can setup a Plan B
So we try to execute a block of code, and if it fails wecatch the error
-
8/13/2019 Dynamic Memory Allocation Exception Handling
19/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
Exceptions allow us as programmers the chance to reactto an exceptional circumstance
When an exception is caught, we throw an exception,which transfers control to another section of code
Catching an exception requires a try block
-
8/13/2019 Dynamic Memory Allocation Exception Handling
20/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
using namespace std;
int main(int argc, const char* argv[]){
try
{
throw new string("I am throwing an exception!!");
}
catch (string *e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
21/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
using namespace std;
int main(int argc, const char* argv[]){
try
{
throw new string("I am throwing an exception!!");
}
catch (string *e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
22/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
using namespace std;
voiddoSomething(int *param)
{
if(param ==NULL)throw new string("'param' cannot be null");
}
int main(int argc, const char* argv[])
{
int *intPointer =NULL;
try{
doSomething(intPointer);
}
catch (string *e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
23/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
using namespace std;
voiddoSomething(int *param)
{
if(param ==
NULL)
throw new string("'param' cannot be null");
}
int main(int argc, const char* argv[])
{
int *intPointer =NULL;
try{
doSomething(intPointer);
}
catch (string *e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
24/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
using namespace std;
voiddoSomething(int *param)
{
if(param ==NULL)
throw new string("'param' cannot be null");
}
int main(int argc, const char* argv[])
{
int *intPointer =NULL;
try{
doSomething(intPointer);
}
catch (string *e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
25/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
using namespace std;
voiddoSomething(int *param)
{
if(param ==NULL)
throw new string("'param' cannot be null");
}
int main(int argc, const char* argv[])
{
int *intPointer =NULL;
try{
doSomething(intPointer);
}
catch (string *e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
26/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
using namespace std;
voiddoSomething(int *param)
{
if(param ==NULL)
throw new string("'param' cannot be null");
}
int main(int argc, const char* argv[])
{
int *intPointer =NULL;
try{
doSomething(intPointer);
}
catch (string *e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
27/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
It is generally good practice to put any dynamic memoryallocations within try blocks
If the memory cannot be allocated for some reason, thenyou can catch it
If this is not caught, your code will continue as if it
worked and either not work as expected or becomeunstable and crash
-
8/13/2019 Dynamic Memory Allocation Exception Handling
28/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
#include
using namespace std;
int main(){
int* intArray;
try
{
intArray = new int[999999999];}
catch (exception &e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
29/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
#include
using namespace std;
int main(){
int* intArray;
try
{
intArray = new int[999999999];}
catch (exception &e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
30/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
#include
using namespace std;
int main(){
int* intArray;
try
{
intArray = new int[999999999];}
catch (exception &e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
31/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
#include
using namespace std;
int main(){
int* intArray;
try
{
intArray = new int[999999999];}
catch (exception &e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
32/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
#include
using namespace std;
int main(){
int* intArray;
try
{
intArray = new int[999999999];}
catch (exception &e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
33/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
#include
using namespace std;
int main(){
int* intArray;
try
{
intArray = new int[999999999];}
catch (exception &e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
34/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
#include
using namespace std;
int main(){
int* intArray;
try
{
intArray = new int[999999999];}
catch (exception &e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
35/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
#include
using namespace std;
int main(){
int* intArray;
try
{
intArray = new int[999999999];}
catch (exception &e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
36/37
Programming in C++ 2011 BlueSignet LLC. All rights reserved.
Exception Handling
#include
#include
using namespace std;
int main(){
int* intArray;
try
{
intArray = new int[999999999];}
catch (exception &e)
{
cout
-
8/13/2019 Dynamic Memory Allocation Exception Handling
37/37
2011 BlueSignet LLC. All rights reserved.
Programming in
C++The End?