You probably want to do:
std::vector<int> scratch(size);
But for the record, what you tried should be:
int* scratch = new int[size];
delete[] scratch;
Note that if size
is known at compile-time, you also simply do:
int scratch[size];
Edit
For an array of pointers to int
, the desired syntax is:
int** scratch = new int*[size];
delete[] scratch;
Note that the the pointers inside the array are not refering to allocated memory and that you must allocate every pointed int
before being able to use them.
That is, you may do:
int a = 3;
int b = 5;
scratch[0] = &a;
scratch[1] = &b;
scratch[2] = &a;
scratch[3] = new int(5); // Well, I don't see why anyone would like to do that but if you do, don't forget to also call delete on the pointer when you are done with it.
Anyway, unless you have some particular constraints, the most C++
way of doing things is probably still a std::vector<int*>
as dealing with pointers of pointers becomes a bit unmaintainable after some levels of indirection.