Friday, July 31, 2009

In C++, if I declare an array on the heap, how do I determine how much elements it has?

You can’t declare an array without knowing how many elements you want it to have. On the heap, you can determine this at runtime, but you must find it somehow. So what’s the problem here?





EDIT:


Nope. Store the array size in a variable. That's the rule for dynamic arrays. Just as a note, you may find the STL vector to be useful. You can determine how many elements it has at runtime. Is it by any chance what you want?





http://www.cplusplus.com/reference/stl/v...

In C++, if I declare an array on the heap, how do I determine how much elements it has?
first of all the way you ar using cant be used.


so


you sud use pointers or as the previous guy said vectors, list etc.


but you cant assign array so simply by taking integer as its size. ie,


int x;


cin%26lt;%26lt;x;


int y[x]; is wrong


for dynamic you must use pointers.


No comments:

Post a Comment