c++ - Deleting array of class objects? -


it's common knowledge code below correctly frees memory of 100 integers.

int* ip = new int[100]; delete [] ip;  

and think user defined classes works:

node* ip = new node[100]; delete [] ip;  
  1. in first case, size of memory freed (400 bytes), determined @ compile time? basically, goes on internally?

  2. in second case, destructor of node called on each of 100 objects?

essentially, have been using syntax, never understood goes on internally , curious.

  1. no. memory allocator invisibly keeps track of size. size cannot determined @ compile time, because allocation not dynamic , following not work:

size_t n; std::cin >> n; = new int[n]; // interesting delete[] a; 
  1. yes. convince of fact, try

struct foo {     ~foo() { std::cout << "goodbye, cruel world.\n"; } };  // in main size_t n; std::cin >> n; foo *a = new foo[n]; delete[] a; 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -