COMP 206 Unit 12: Dynamic Object Creation

Descrição

University-level C++ COMP 206 FlashCards sobre COMP 206 Unit 12: Dynamic Object Creation, criado por Adriana Vincelli-Joma em 15-10-2021.
Adriana Vincelli-Joma
FlashCards por Adriana Vincelli-Joma, atualizado more than 1 year ago
Adriana Vincelli-Joma
Criado por Adriana Vincelli-Joma aproximadamente 3 anos atrás
3
0

Resumo de Recurso

Questão Responda
Two Events that Occur When Object is Created 1. storage is allocated for object 2. constructor is called to initialize that storage
Three Ways in Which First Event Occurs 1. storage can be allocated before program begins in static storage area 2. storage can be created on stack whenever execution point is reached (opening brace) 3. storage can be allocated from pool of memory (heap/free storage)
C++ does not employ C language approach to heap - C dynamic memory functions are confusing and complicated - C programmers who use virtual memory machines allocating huge arrays of variables in static storage area to avoid dynamic memory allocation
operator new - allocates enough storage on heap to hold object and calls constructor for storage - makes sure memory allocation was successful before passing address to constructor
delete expression call destructor and then releases memory
operator delete called for objected created by new
Overhead Used by Memory Manager - size of objects and lifetime is built right into generated code - call malloc(), which requests block of memory from pool - pool is searched for block of memory large enough to satisfy request - size and location of block must be recorded so further calls to malloc() won't use it
Using Delete for void* - creates bug in program, unless destination of pointer is simple - pointer should not have destructor - when object is manipulated, storage is released but destructor is not called
operator[] returns pointer but leaves it as member of container
remove() returns pointer but also removes it from container by assigning position to 0
Responsibility for cleanup when using pointers - container cannot "own" pointer because it holds it as a void* and thus cannot perform proper cleanup - user must be responsible for cleaning up objects
new with array - must be default constructor, except for aggregate initialization on stack - constructor with no arguments must be called for every object - e.g MyType* fp = new MyType[100] - allocates enough storage on heap for 100 MyType objects and calls constructor for each one
delete with array - e.g. delete []fp - empty brackets tells compiler to generate code that fetches number of objects in array, stored somewhere when array is created, and calls destructor for that array objects
Converting pointer to operate like array - define pointer as constant so any attempt to modify pointer will be flagged as error - e.g. int* const q = new int[10]
new handler - pointer to function is checked, and if pointer is nonzero, then function it points to is called - default behavior is to throw exception - must take no arguments and have a void return value
operator new() - allocates storage and then constructor is called - when overloading, replace behavior when it runs out of memory: * return 0 *write loop to call new-handler * retry allocation - e.g void* operator new(size_t size); * throw bad_alloc exception
operator delete() - deallocates storage - e.g. void operator delete(void*);
overloading global new and delete - operator new must take arg size_t - return value of new is void* -operator delete takes void* to memory that was allocated by operator new -gets pointer after destructor is called
overloading new and delete for class - local operator new() has same syntax as global - new() searches through allocation map looking for false, then sets location to true to indicate it's been allocated and returns address of corresponding memory block - operator delete() calculates block in pool that pointer represents, then sets allocation map's flag for block to false to indicate block has been released
overloading new and delete for array - operator new[](size_t sz) - operator delete[](void* p) -global new() called to allocate storage for array - global delete() called to release storage
Two less common uses for overloading operator new() 1. place object in specific location in memory 2. choose from different allocation when calling new

Semelhante

COMP 206 Unit 11: Operator Overloading
Adriana Vincelli-Joma
COMP 206 Unit 9: Name Control
Adriana Vincelli-Joma
COMP 206 Unit 6: Initialization and Cleanup
Adriana Vincelli-Joma
COMP 206 Unit 13: Inheritance and Composition
Adriana Vincelli-Joma
COMP 206 Unit 5: Hiding the Implementation
Adriana Vincelli-Joma
COMP 206 Unit 7: Function Overloading and Default Arguments
Adriana Vincelli-Joma
COMP 206 Unit 8: Constants
Adriana Vincelli-Joma
COMP 206 Unit 3: The C in C++
Adriana Vincelli-Joma
GOVN 377 Unit 8: The Brave New World of Data Management and Manipulation
Adriana Vincelli-Joma
HIST 404: Unit 4 Science in the Renaissance
Adriana Vincelli-Joma
HIST 404: Unit 9 Discovering DNA
Adriana Vincelli-Joma