Memory Management with 'new'

Sometimes it is useful to take memory management in our control to make sure we have the right amount of memory already reserved in advance. This could be to speed up memory allocation/deallocation or for debugging purpose where contiguous memory allocation can speed up debugging or for variety of reasons.

The following example shows one way in which memory can be reserved in a chunk for the program.



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>

using namespace
std;

class
Class
{

public
:
int
x;
char
y;
bool
z;
};


int
main()
{

unsigned char
tempBuf[100];
cout<<"Pointer for tempBuf = " << &tempBuf << endl;

Class* c = new (tempBuf) Class;
cout<<"Pointer for c = " << c << endl;

return
0;
}




The output is as follows:

Check out this stream