Code1:
int * a;
a=new int[222];
cout<<"the size of a is "<<sizeof(a)<<endl;
Output1:
the size of a is 8
Code2:
int * a;
a=new int[222];
cout<<"the size of a is "<<a.length()<<endl;
Output2:
error: member reference base type 'int *' is not a structure or
union
How to get the size of a heap array? Thanks..
The short answer is, it can't be done, at least not in a portable manner. You'll need to keep track of the array's size separately somehow (e.g. in an integer variable or similar).
Use a container instead of a raw pointer:
std::vector<int> a(222);
cout << "the size of a is " << a.size(); << endl;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With