I am compiling using g++. This is valid but I want to convert to the C++ operators new() and delete() because this is considered best practice.
int *scratch = (int *)malloc(size * sizeof(int));
free(scratch);
What is the equivalent using new and delete? This is my guess.
int **scratch = new int*[size];
delete[] scratch;
This is for an array of pointers.
The double star is a reference. It is a pointer to a pointer.
You probably want to do:
But for the record, what you tried should be:
Note that if
sizeis known at compile-time, you also simply do:Edit
For an array of pointers to
int, the desired syntax is:Note that the the pointers inside the array are not refering to allocated memory and that you must allocate every pointed
intbefore being able to use them.That is, you may do:
Anyway, unless you have some particular constraints, the most
C++way of doing things is probably still astd::vector<int*>as dealing with pointers of pointers becomes a bit unmaintainable after some levels of indirection.