在c ++中获取堆数组的大小

spi*_*oyo 2 c++ arrays heap

代码1:

int * a;
a=new int[222];
cout<<"the size of a is "<<sizeof(a)<<endl;
Run Code Online (Sandbox Code Playgroud)

输出1:

the size of a is 8
Run Code Online (Sandbox Code Playgroud)

代码2:

int * a;
a=new int[222];
cout<<"the size of a is "<<a.length()<<endl;
Run Code Online (Sandbox Code Playgroud)

输出2:

error: member reference base type 'int *' is not a structure or
  union
Run Code Online (Sandbox Code Playgroud)

如何获得堆数组的大小?谢谢..

Jer*_*ner 5

简短的回答是,它不能完成,至少不能以便携方式完成.您需要以某种方式单独跟踪数组的大小(例如,在整数变量或类似变量中).