New blog location
New blog location
int foo() { double d = 3.0; return 0; }
d
is deallocated when foo
's scope ends. When memory is allocated on the heap, the picture looks like this. The pointer lives on the stack and the allocated memory is on the heap.int foo() { double * dptr = new double; }
float **ptrToPtr = new float*; *ptrToPtr = new float;
new
and delete
malloc
and free
from C as they have no concept of object initialization and destruction. Use new
and delete
instead. Use new
to allocate memory and delete
to deallocate memory. When using raw pointers every call to new
must be paired with a call to delete
. The call to new
can fail if the system is out of memory, so there has to be some way to handle this situation.int ary[5] = {0};
#include <iostream> using namespace std; int main() { int numElems = 0; cout << "Enter the number of elements for the array" <<endl; cin >> numElems; int* aryPtr = new int[numElems]; }
new[]
) should be paired with the array version of delete (delete[]
) as follows:delete[] aryPtr; aryPtr = nullptr;
class Basic { public: Basic() {cout << "Basic constructor called" << endl;} ~Basic() {cout << "Basic destructor called" << endl;} };
Basic
objects, the Basic
constructor is called 3 times:Basic* basicAryPtr = new Basic[3]; /*Use basicAryPtr here */ delete[] basicAryPtr; basicAryPtr = nullptr;
new
and delete
in a loop as shown below:int numElems = 3; Basic **ptrToBasicPtrs = new Basic* [numElems]; for(int k = 0; k < numElems; k++) { ptrToBasicPtrs[k] = new Basic(); } //Use the objects for(int k = 0; k < numElems; k++) { delete ptrToBasicPtrs[k]; } //delete the stack pointer delete[] ptrToBasicPtrs; ptrToBasicPtrs = nullptr;
double matrix[3][3];
double** allocateMatrix(int numRows, int numCols) { double** myMat = new double*[numRows]; for(int i = 0; i < numRows; i++) { myMat[i] = new double[numCols]; } return myMat; }
void deallocateMatrix(double** myMat, int numRows) { for(int i = 0; i < numRows ; i++) { delete[] myMat[i]; //Delete columns } delete[] myMat; //delete rows }
vector<T>
instead of plain arrays. For two dimensional arrays, use vector<vector<double>>
for example.int
s or double
s for example. Casts can be performed on pointers through the following:XMLDocument* docPtr = getXMLDocument(); char* charPtr = (char*)docPtr;
static_cast
, the compiler enforces the rule that the types should be related:XMLDocument *docPtr = getXMLDocument(); char *charPtr = static_cast<char*>(docPtr);
int myAry[3] = {0}; *(myArray) = 1; *(myArray+1) = 2; *(myArray+2) = 3;
myArray[0] = 1; myArray[1] = 2; myArray[2] = 3;
std::unique_ptr
takes single ownership of the resource and deallocates the underlying memory when it goes out of scope or is reset. When ownership is shared, use std::shared_ptr
which uses reference counting to keep track of all references to the object and only deallocates memory when the last reference is removed.new
and delete
can cause problems. For example, consider the following:Basic* basicPtr = new Basic(); basicPtr->doWork(); delete basicPtr;
doWork()
throws an exception, the delete
statement is not executed, causing a memory leak. A std::unique_ptr
should be used in this case. When the pointer goes out of scope or if an exception is thrown, the underlying memory is deallocated. Use make_unique
to define a unique_ptr
:auto myPtr = make_unique<Basic>(); auto myPtr2 = make_unique<Basic>(p1,p2); //constructor parameter variant.
uniwue_ptr
represents single ownership it cannot be copied. However, the responsibility can be moved as shown in this article link, using std::move
.std::shared_ptr
.auto owner1 = make_shared<Basic>(); std::shared_ptr<Basic> owner2(owner1);
Comments
Post a Comment