My C++ program needs a block of uninitialized memory and a void*
pointer to that block so that I can give it to a third party library. I want to pass control of
The question cannot be answered sensibly.
Firstly, it is said that the program 'needs' a block of uninitialized memory but, from the code sample given, it seems that the program 'needs' a block of uninitialized and UNTYPED memory which seems not very C++ or OO.
Secondly, a std::vector gives sole and automatic control over a block of typed memory that may or may not change in size according to its use. You can lose this control if an instance of std::vector is created on the heap and tracked with raw pointers just as for any other C or C++ object such as a void* memory block.
Thirdly, what is the intended use of this memory block? The answer to this may or may not dictate the use of operator new or operator new[]. In the design of this program, is there a single interpretation of this memory block? What ownership semantics do you require, if any? Etc, etc.
Use new
with a single object and new[]
with an array of objects. So, for example:
int* x = new int; // Allocates single int int* y = new int[5]; // Allocates an array of integers *x = 10; // Assignment to single value y[0] = 8; // Assignment to element of the array
If all you are doing is allocating a memory buffer, then allocate an array of char
as in:
int bufferlen = /* choose a buffer size somehow */ char* buffer = new char[bufferlen]; // now can refer to buffer[0] ... buffer[bufferlen-1]
However, in C++, you should really use std::vector
for arbitrary arrays, and you should use std::string
for character arrays that are to be interpreted or used as strings.
There is no reason to invoke ::operator new
or ::operator new[]
explicitly rather than using the ordinary syntax for these calls. For POD and primitive types (e.g. char
) no initialization will take place. If you need to get a void*
buffer, then simply use static_cast
to convert char*
to void*
.
The advantage of the C++ new
operators over C's malloc()
and free()
is that the former throws an exception if there is not enough memory, rather than returning NULL
.
Regarding choosing new(size) and new[] for character buffers, I'd advocate the latter since it is less likely to surprise people maintaining the code later i.e. char* buf = new char[size]
and delete[] buf
.
The values in the buffer will not be initialised, and there is no range-checking - you have to build a nice C++ object to do that for you, or use an existing object such as std::vector
or std::string
.
for allocating memory to array/list use new[] other than that use new...