BCB6.0+pack4有个大bug!被我发现了.
测试平台: win2000 server 简体中文版 + sp3/sp4
在dll中动态分配二维数组,
CMatrix::CMatrix()
{
this->m_nRow = 0;
this->m_nCol = 0;
this->m_pMatrix = 0;
}
CMatrix::CMatrix(const unsigned long nRow, const unsigned long nCol)
{
m_nRow = nRow;
m_nCol = nCol;
if (m_nRow == 0 || m_nCol == 0)
{
m_nRow = 0;
m_nCol = 0;
m_pMatrix = 0;
}
else
{
// Dynamic allocate memory
m_pMatrix = new (double *)[m_nRow];
for (unsigned long i = 0;
i < m_nRow;
i++)
{
m_pMatrix = newdo
uble[m_nCol];
}
}
}
CMatrix::CMatrix(const CMatrix&
matrix)
{
this->m_nRow = matrix.m_nRow;
this->m_nCol = matrix.m_nCol;
if (this->m_nRow == 0 || this->m_nCol == 0)
{
this->m_nRow = 0;
this->m_nCol = 0;
this->m_pMatrix = 0;
}
else
{
this->allocate_memory();
for(unsigned long i = 0;
i < this->m_nRow;
++i)
{
for(unsigned long j=0;
j < this->m_nCol;
++j)
{
this->m_pMatrix[j] = matrix.m_pMatrix[j];
}
}
}
}
在调用时,
void main()
{
string strFileName = "data.txt";
CMatrix matrixA(2,2);
CMatrix matrixB(2,2);
cout<<matrixA.GetMatrixRow()<<endl;
// 运行到这里时行数会发生突变.
cout<<matrixA.GetMatrixCol()<<endl;
}
如果注释掉 CMatrix matrixB(2,2);
则不存在此问题.
注意做成dll形式,如果不做成dll形式则不存在此问题.
而同样代码VC6不存在此问题.