19
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]?

Ziezi
  • 6,375
  • 3
  • 39
  • 49
Brandon
  • 22,723
  • 11
  • 93
  • 186
  • 7
    Why do you want a `void *` in C++? – Oliver Charlesworth Jan 01 '13 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 01 '13 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))` –  Jan 01 '13 at 16:07

5 Answers5

38

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.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
22

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.
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Ok but why does Malloc allow it then? Void is still not an object in C. – Brandon Jan 01 '13 at 16:06
  • 1
    @CantChooseUsernames: `malloc` does not allow this... The analogous\ `malloc` invokation would be `malloc(sizeof(void)*N)`, which will not compile. – Yakov Galka Jan 01 '13 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 *`. – Oliver Charlesworth Jan 01 '13 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). – Dietmar Kühl Jan 01 '13 at 16:26
12

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)

David G
  • 94,763
  • 41
  • 167
  • 253
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
3

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++.

David G
  • 94,763
  • 41
  • 167
  • 253