Exceptions and Memory (4) Flashcards
What is the purpose of exception handling?
Permit the program to catch and handle errors, rather than let the error occur and suffer the consequences.
What is a try block?
It encloses code that may generate an error and produce and exception
Use as few as possible
What is a catch block?
Follows a try block
Specifies the type of exception it can catch and handle
Contains an exception handler
What is the terminate function?
Calls the abort function that aborts the program
Called if there is no catch block
How does throw work with const objects?
If a func throws a const object then the catch handler must be declared const as well
What is the Data segment of memory?
Allocated at compilation time and is of fixed size
When is memory allocated in the stack?
Automatically managed at run time
When is memory allocated in the heap?
Explicitly managed by the program
What is static memory?
Reserved at compile time in data segment
Deallocated when program exits
Ex: Global vars, statics vars, static class members, variables allocated outside of a function or using Static
Automatically set to 0
What is automatic memory?
Allocated at run time as control flow enters and deallocated as flow exits
Function params, local variables, added to stack and deallocated in reverse order
What is dynamic memory?
Allocated during run time
Maintained on the memory heap
Requires use of new and delete
More flexible
When does a memory leak occur?
When memory is allocated from the heap using new but is not returned to the heap using delete
What order does delete [] arr delete things?
Deallocates in the opposite direction from the allocation (from back to front)
What does new operator do?
Calls the function operator new to allocate storage
Invokes a constructor to turn the uninitialized storage into an object