以前VC6,一直以为动态分配一个大小不确定的数组是不行的,现在用vs2003.net发现可以,如:
#include <iostream>
using namespace std;
class stu
{
public:
stu(int aa=1,int bb=2)
{
a=aa;
b=bb;
}
int a;
int b;
};
int main()
{
int a=10;
//cin >>a;
stu*p = new stu[a];
for(int i=0; i<a; i++)
{
cout <<p[i].a <<" " <<p[i].b<<endl;
}
}
今天用delete释放动态分配的数组遇到问题,情形是这样的:
class AAAA
{
public:
int a;
int b;
}
class BBBB
{
public:
BBBB()
{
num =2;
}
AAAA*m_a;
int num;
void func()
{
ASSERT(num>0);
if(m_a!=NULL)
delete m_a;
else
m_a = new AAAA[num]
}
}
main()
{
BBBB b;
b.func();
}
编译没错误,运行就出错了,瞧了半天没瞧出来,网上也查了半天,你看出来没,答案是:delete [] m_a;