O GoConqr está de visual novo! Conte para nós o que você achou.

Flashcards

COMP 206 Unit 12: Dynamic Object Creation

Adriana Vincelli-Joma COMP 206 University-level C++ Inglês 21 flashcards

Flashcards do conjunto (21)

  1. 1Two Events that Occur When Object is Created1. storage is allocated for object
    2. constructor is called to initialize that storage
  2. 2Three Ways in Which First Event Occurs1. 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)
  3. 3C++ 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
  4. 4operator 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
  5. 5delete expressioncall destructor and then releases memory
  6. 6operator deletecalled for objected created by new
  7. 7Overhead 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
  8. 8Using 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
  9. 9operator[]returns pointer but leaves it as member of container
  10. 10remove()returns pointer but also removes it from container by assigning position to 0
  11. 11Responsibility 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
  12. 12new 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
  13. 13delete 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
  14. 14Converting 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]
  15. 15new 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
  16. 16operator 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
  17. 17operator delete()- deallocates storage
    - e.g. void operator delete(void*);
  18. 18overloading 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
  19. 19overloading 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
  20. 20overloading 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
  21. 21Two less common uses for overloading operator new()1. place object in specific location in memory
    2. choose from different allocation when calling new

Informações

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.
Visualizações
13
Avaliação

Fale Conosco

Sobre o que é sua mensagem?

O que você acha do novo visual?

Você gostou?