Академический Документы
Профессиональный Документы
Культура Документы
Destroyed with
The operator delete Similar to free
Programming in C++
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
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
Programming in C++
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
Programming in C++
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
Programming in C++
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
#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++
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++
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++
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++
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++
#include <iostream> using namespace std; int main() { string *str = new string("This is my string"); delete str; return 0; }
Programming in C++
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
Programming in C++
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++
Generally, it is generally good practice to use heap for primitive data types and Free Space for objects
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
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
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
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
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
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
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
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
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
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?