21
int main()
{
    void* Foo = new???
    delete Foo;
}

How do you do something like the above? You can't put new void[size]. And I don't want to know how to do it with malloc() and free(). I already know that works. I'm curious and want to know how it's done with new and delete.

I googled this and saw something about operator new(size); and operator delete(size);

What is the difference between those and new / delete? Why does C++ not just allow new void* [size]?

3
  • 7
    Why do you want a void * in C++? Jan 1, 2013 at 16:04
  • 1
    I'm just curious because I see it being done in C. In C, I saw someone do void* Foo = malloc(size); It's the same thing no?
    – Brandon
    Jan 1, 2013 at 16:05
  • 2
    No it's not. In C++ new type[3] allocates 3 times the size of type where malloc would have to look something like this: malloc(3*sizeof(type))
    – user238801
    Jan 1, 2013 at 16:07

5 Answers 5

39

This will do the trick:

int main()
{
    void* Foo = ::operator new(N);
    ::operator delete(Foo);
}

These operators allocate/deallocate raw memory measured in bytes, just like malloc.

23

Why does C++ not just allow new void[size]?

Because void is not an object; it has no size! How much space should be allocated? Bear in mind that new T[size] is approximately equivalent to malloc(sizeof(T) * size).

If you just want a raw byte array, then you could use char.*


* Although, of course, because this is C++ you should use something like std::vector<char> to avoid memory-leak and exception-safety issues.

4
  • Ok but why does Malloc allow it then? Void is still not an object in C.
    – Brandon
    Jan 1, 2013 at 16:06
  • 1
    @CantChooseUsernames: malloc does not allow this... The analogous\ malloc invokation would be malloc(sizeof(void)*N), which will not compile. Jan 1, 2013 at 16:07
  • 1
    @CantChooseUsernames: It doesn't "allow" it; malloc is not typesafe; in order to be able to assign it to any pointer type, it needs to return a void *. Jan 1, 2013 at 16:07
  • 10
    I don't think raw memory should use new char[n]: the allocated memory contains objects of type char. Raw memory is allocated using operator new(n) or operator new[](n) (or using suitable allocator members). Jan 1, 2013 at 16:26
13

C++ travels in constructed objects allocated using some variation of new T. or new T[n] for some type T. If you really need uninitialized memory (it is very rare that you do), you can allocate/deallocate it using operator new() and operator delete():

void* ptr = operator new(size);
operator delete(ptr);

(similarily for the array forms)

4

You could do

void* data = new char[num_of_bytes];

to allocate some memory, in a similar fashion as malloc

2

void * is convertible to any pointer type. You can simply do void *Foo = new int or any other type that you want. But there really isn't a reason to do this in C++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.