Reputation: 1499
A simple question, though I cant find an anwer in the quicker time.
I am using the Dynamic allocation through 'new' keyword for the allocation of chunck of memory for an array, as
int *array = new int[size]; //The Size is got by some logic
And now on the long run of the logic, I need to parse this array such that i need to get/calculate the size of this array.
I am clueless at here. Please help me out.
Thanks in Advance :)
Upvotes: 0
Views: 187
Reputation: 20282
You need to keep the allocation size in a separate variable to know what it was after you're done with the new
.
On the bright side - C++ offers you a tool called std::vector
that would solve the problem for you without using new
and keeping the size (and without being restricted by the size, as well).
So if you need to preallocate the size you can use:
vector<int> array(size);
But normally you'd just declare
vector<int> array;
and then go on and fill it, and query array.size()
to know how many items there are.
Upvotes: 8