Cpp - Pointer new Keyword


You can allocate memory in C++ by using the new keyword followed by the type of the object.

The statement 'new unsigned short int' allocates two bytes in the heap, and new long allocates four.

The return value from new is a memory address. It must be assigned to a pointer.

To create an unsigned short on the heap, you might write the following:

unsigned short int *pPointer; 
pPointer = new unsigned short int; 

You can initialize the pointer at its creation:

unsigned short int *pPointer = new unsigned short int; 

pPointer now points to an unsigned short int on the heap. You can assign a value into that area of memory:

*pPointer = 72;