Showing posts with label Allocating Void Pointer memory. Show all posts
Showing posts with label Allocating Void Pointer memory. Show all posts

Wednesday, 13 September 2017

Allocating Void pointer memory using new operator

I was Implementing Generic List in C++ and was able to successfully allocate memory of void*  data using malloc ( malloc return void *) . But how about using new. Can we do below


void Test()
{
           void *f= new void[25];  //void *f=malloc(25); --> Works
           delete f;
}


The Answer is NO.
Because void has no size . How much space has to be allocated is not known.

So if you need to allocate memory using new use operator like


void Test()
{
          void *f=operator new(25);
          delete f;
}