Вы находитесь на странице: 1из 37

Programming in C++

Dynamic Memory Allocation & Exception Handling

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

2011 BlueSignet LLC. All rights reserved.

Programming in C++

Dynamic Memory Allocation


#include <iostream> #include <stdlib.h> 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 << integerArray[i] << endl; delete[] integerArray; return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++

Dynamic Memory Allocation


#include <iostream> #include <stdlib.h> using namespace std;

integerArray

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 << integerArray[i] << endl; delete[] integerArray; return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++
>g++ dynmem.cpp o dynmem.exe >dynmem.exe 3

Dynamic Memory Allocation


#include <iostream> #include <stdlib.h> using namespace std;

integerArray

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 << integerArray[i] << endl; delete[] integerArray; return 0; }
2011 BlueSignet LLC. All rights reserved.

0 1 2

>g++ dynmem.cpp o dynmem.exe >dynmem.exe 3

Programming in C++

Dynamic Memory Allocation


#include <iostream> #include <stdlib.h> using namespace std;

integerArray

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 << integerArray[i] << endl; delete[] integerArray; return 0; }
2011 BlueSignet LLC. All rights reserved.

0 1 2

>g++ dynmem.cpp o dynmem.exe >dynmem.exe 3 0 1 2

Programming in C++

Dynamic Memory Allocation


#include <iostream> #include <stdlib.h> using namespace std;

integerArray

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 << integerArray[i] << endl; delete[] integerArray; return 0; }
2011 BlueSignet LLC. All rights reserved.

0 1 2

>g++ dynmem.cpp o dynmem.exe >dynmem.exe 3 0 1 2

Programming in C++

Dynamic Memory Allocation


#include <iostream> #include <stdlib.h> using namespace std;

integerArray

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 << integerArray[i] << endl; delete[] integerArray; return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++
>g++ dynmem.cpp o dynmem.exe >dynmem.exe 3 0 1 2

Dynamic Memory Allocation

#include <iostream> using namespace std; int main() { string *str = new string; *str = "This is my string"; delete str; return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++

Dynamic Memory Allocation

#include <iostream> using namespace std;

str

int main() { string *str = new string; *str = "This is my string"; delete str; return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++

Dynamic Memory Allocation

#include <iostream> using namespace std;

str
01001 11011

int main() { string *str = new string; *str = "This is my string"; delete str; return 0; }
2011 BlueSignet LLC. All rights reserved.

Programming in C++

Dynamic Memory Allocation

#include <iostream> using namespace std;

str
01001 11011

int main() { string *str = new string; *str = "This is my string"; delete str; return 0; }
2011 BlueSignet LLC. All rights reserved.

Programming in C++

Dynamic Memory Allocation

#include <iostream> using namespace std;

str

int main() { string *str = new string; *str = "This is my string"; delete str; return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++

Dynamic Memory Allocation

#include <iostream> using namespace std; int main() { string *str = new string("This is my string"); delete str; return 0; }

2011 BlueSignet LLC. All rights reserved.

Programming in C++

Dynamic Memory Allocation

Although new and delete act like keywords, they are actually operators When new is called, memory is allocated from a memory pool called the Free Store malloc and calloc use the Heap

2011 BlueSignet LLC. All rights reserved.

Programming in C++

Dynamic Memory Allocation

Free Store Allocates with new Deallocates with delete

Heap Uses malloc / calloc Uses free

Type Identification returns Returns a void pointer a pointer with designated type Automatically handles allocation size Can be overwritten with method overridding
2011 BlueSignet LLC. All rights reserved.

Requires the block size to be passed malloc / alloc / free methods are all static
Programming in C++

Dynamic Memory Allocation

Generally, it is generally good practice to use heap for primitive data types and Free Space for objects

2011 BlueSignet LLC. All rights reserved.

Programming in C++

Exception Handling

This allows us to attempt potentially dangerous Plan A code, and if anything fails we can setup a Plan B So we try to execute a block of code, and if it fails we catch the error

2011 BlueSignet LLC. All rights reserved.

Programming in C++

Exception Handling

Exceptions allow us as programmers the chance to react to 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

2011 BlueSignet LLC. All rights reserved.

Programming in C++

Exception Handling

#include <iostream> using namespace std; int main(int argc, const char* argv[]) { try { throw new string("I am throwing an exception!!"); } catch (string *e) { cout << "An exception occurred: " << *e << endl; } return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++

Exception Handling

#include <iostream> using namespace std;

An exception occurred: I am throwing an exception!!

int main(int argc, const char* argv[]) { try { throw new string("I am throwing an exception!!"); } catch (string *e) { cout << "An exception occurred: " << *e << endl; } return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++

Exception Handling
#include <iostream> using namespace std; void doSomething(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 << "An exception occurred: " << *e << endl; } return 0; }

intPointer

2011 BlueSignet LLC. All rights reserved.

Programming in C++

Exception Handling
#include <iostream> using namespace std; void doSomething(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 << "An exception occurred: " << *e << endl; } return 0; }

intPointer

2011 BlueSignet LLC. All rights reserved.

Programming in C++

Exception Handling
#include <iostream> using namespace std; void doSomething(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 << "An exception occurred: " << *e << endl; } return 0; }

intPointer

2011 BlueSignet LLC. All rights reserved.

Programming in C++

Exception Handling
#include <iostream> using namespace std; void doSomething(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 << "An exception occurred: " << *e << endl; } return 0; }

intPointer

2011 BlueSignet LLC. All rights reserved.

Programming in C++

Exception Handling
#include <iostream> using namespace std; void doSomething(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 << "An exception occurred: " << *e << endl; } return 0; } An exception occurred: 'param' cannot be null

intPointer

2011 BlueSignet LLC. All rights reserved.

Programming in C++

Exception Handling

It is generally good practice to put any dynamic memory allocations within try blocks If the memory cannot be allocated for some reason, then you can catch it If this is not caught, your code will continue as if it worked and either not work as expected or become unstable and crash

2011 BlueSignet LLC. All rights reserved.

Programming in C++

Exception Handling
#include <iostream> #include <exception> using namespace std;

intArray int main() { int* intArray; try { intArray = new int[999999999]; } catch (exception &e) { cout << Caught exception: " << e.what() << endl; } return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++

Exception Handling
#include <iostream> #include <exception> using namespace std;

intArray int main() { int* intArray; try { intArray = new int[999999999]; } catch (exception &e) { cout << Caught exception: " << e.what() << endl; } return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++

Exception Handling
#include <iostream> #include <exception> using namespace std;

intArray int main() { int* intArray; try { intArray = new int[999999999]; } catch (exception &e) { cout << Caught exception: " << e.what() << endl; } return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++

Exception Handling
#include <iostream> #include <exception> using namespace std;

intArray int main() { int* intArray; try { intArray = new int[999999999]; } catch (exception &e) { cout << Caught exception: " << e.what() << endl; } return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++

Exception Handling
#include <iostream> #include <exception> using namespace std;

intArray int main() { int* intArray; try { intArray = new int[999999999]; } catch (exception &e) { cout << Caught exception: " << e.what() << endl; } return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++

Exception Handling
#include <iostream> #include <exception> using namespace std;

intArray int main() { int* intArray; try { intArray = new int[999999999]; } catch (exception &e) { cout << Caught exception: " << e.what() << endl; } return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++

Exception Handling
#include <iostream> #include <exception> using namespace std;

intArray int main() { int* intArray; try { intArray = new int[999999999]; } catch (exception &e) { cout << Caught exception: " << e.what() << endl; } return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++

Exception Handling
#include <iostream> #include <exception> using namespace std;

intArray int main() { int* intArray; try { intArray = new int[999999999]; } catch (exception &e) { cout << Caught exception: " << e.what() << endl; } return 0; }
2011 BlueSignet LLC. All rights reserved. Programming in C++

Exception Handling
#include <iostream> #include <exception> using namespace std;

intArray int main() { int* intArray; try { intArray = new int[999999999]; } catch (exception &e) { cout << Caught exception: " << e.what() << endl; } return 0; Caught exception: bad_alloc }
2011 BlueSignet LLC. All rights reserved. Programming in C++

Programming in C++
The End?

2011 BlueSignet LLC. All rights reserved.

Вам также может понравиться