Review C++ Grammar--Memory Management

Posted by suntra on Sat, 13 Jun 2020 18:28:16 +0200

1, Draw Memory

2, new/malloc and delete/free

new, delete are operators
The malloc() function is responsible for leaving only a certain amount of memory, and it does not know or care about the object itself.Calling new not only allocates the correct size of memory, but also calls the corresponding constructor to build the object.
free() and delete are similar in that delete calls a destructor to clean up the object.

3. Dynamic memory allocation and release of pointer arrays

const size_t size = 4;
Simple** mySimplePtrArray = new Simple*[size];
for (size_t i = 0; i < size; i++)
      mySimplePtrArray[i] = new Simple();
for (size_t i = 0; i < size; i++)
      delete mySimplePtrArray[i];
delete [] mySimplePtrArray;
mySimplePtrArray = nullptr;

4. Dynamic memory allocation and release for multidimensional arrays

char** board = new char[i][j];  // Error!Multidimensional array memory layout is not contiguous, so the stack-based method of allocating sufficient memory for multidimensional arrays is incorrect
// Demonstrate correctly
char** allocateCharBoard(size_t x, size_t y)
      char** myArray = new char*[x];
      for (size_t i = 0; i < x; i++)
            myArray[i] = new char[y];
      return myArray;

void releaseCharBoard(char** myArray, size_t x)
      for (size_t i = 0; i < x; i++)
            delete [] myArray[i];
      delete [] myArray;

5, Smart Pointer

// Use standard smart pointer unique_ptr and shared_ptr requires a header file <memory>

// Create unique_ptr
void couldBeLeaky()
      Simple* mySimplePtr = new Simple();
      mySimplePtr -> go();  // If an exception is thrown, the following delete will not execute, causing a memory leak
      delete mySimplePtr;

void notLeaky()
      auto mySimpleSmartPtr = make_unique<Simple>();  // If the constructor requires parameters, place it in ()
      mySimpleSmartPtr -> go();

// Use unique_ptr

// a get() method can be used to directly access the underlying pointer
void processData(Simple* simple) {};
auto mySimpleSmartPtr = make_unique<Simple>();

// b reset() releases the underlying pointer and changes it to another pointer
mySimpleSmartPtr.reset();  // Release and assign nullptr
mySimpleSmartPtr.reset(new Simple());  // Release and set to a new pointer

// c release() disconnects from the underlying pointer and sets the smart pointer to nullptr
Simple* simple = mySimpleSmartPtr.release();
delete simple;
simple = nullptr;

// d Create a C-style array
auto arr = make_unique<int[]>(10);
// Create shared_ptr
auto smartPtr = make_shared<Simple>();

// a and unique_ptr-like pointer for storing dynamically assigned C-style arrays
// b also supports get() and reset(), the only difference being that when reset() is called, only the last shared_Release underlying resources only when PTR is destroyed or reset
// c does not support release(), use_count() to retrieve shared_of the same resourceNumber of PTR instances
// d Reference Count avoids double deletion

Topics: C++