memory allocation in C++

c++, memory, new-operator

Solution

Arbitrary memory blocks can be allocated with `operator new` in C++; not with the `new` operator which is for constructing objects.

void* pBlock = ::operator new(7);

Such blocks can subsequently be freed with `operator delete`.

::operator delete(pBlock);

Note that `operator new` will allocated memory suitably aligned for any sort of object, so the implementation might not allocate exactly seven bytes and no more, but the same is (usually) true of `malloc`. C clients of `malloc` usually need aligned memory too.

Problem

Is it possible to allocate an arbitrary memory block using the "new" operator? In C I can do it like "void * p = malloc(7);" - this will allocate 7 bytes if memory alignment is set to 1 byte. How to make the same in C++ with the new operator?

Original source