win10,vs2022preview
目的:将二维指针p指向的空间拓宽,并按一定顺序赋值
思路:新定义另一个二维指针tempP,将p指向空间的中元素赋给tempP指向空间的元素,然后释放p指向的空间,把tempP的值赋给p,让tempP指向NULL
报错内容:
严重性 代码 说明 项目
警告 C6385 正在从 "tempP[r]" 读取无效数据。
OriData** p = new OriData*[1];
p[0] = new OriData[1];
int r = 0;
int *s =new int[1];
s[0] = 0;
p[0][0] = oridataP[0];
for (int i = 1; i < OriArrSize; i++)
{
if ((p[r][s[r]]).GetComName() == (oridataP + i)->GetComName())
{
int sum1 = (p[r][s[r]]).GetsumMonth();
int sum2 = (oridataP + i)->GetsumMonth();
if ((sum2 - sum1) > 0)
{
OriData** tempP = new OriData*[r + 1];
for (int m = 0; m < r; m++)
{
tempP[m] = new OriData[s[m]+1];
for (int n = 0; n < s[m] + 1; n++)
{
tempP[m][n] = p[m][n];
}
}
tempP[r] = new OriData[s[r] + 1 + sum2 - sum1];
int N = 1;
for (int n = 0; n < s[r] + 1 + sum2 - sum1; n++)
{
if (n < s[r] + 1)
{
tempP[r][n] = p[r][n];
}
else if (n >= s[r] + 1 && n < s[r] + 1 + sum2 - sum1)
{
tempP[r][n] = p[r][s[r]];//这里报错!!!!!!!!
(tempP[r][n]).SetMonth((tempP[r][n]).GetMonth() + N);
N++;
}
}
for (int m = 0; m < r; m++)//释放p管理的内存
{
delete[] p[m];
}
delete[] p;
p = tempP;//p接管tempP管理的内存
tempP = NULL;
s[r] += sum2 - sum1;
}
p[r][s[r]] = p[r][s[r]] + *(oridataP + i);
}
else
{
OriData** tempP = new OriData * [r + 1 + 1];
for (int m = 0; m < r + 1; m++)
{
tempP[m] = new OriData[s[m]+1];
for (int n = 0; n < s[m]+1; n++)
{
tempP[m][n] = p[m][n];
}
}
int* temp = new int[r + 1 + 1];
for (int n = 0; n < r + 1 ; n++)
{
temp[n] = s[n];
}
temp[r+1] = 0;
delete[] s;
s = temp;
temp = NULL;
for (int m = 0; m < r+1; m++)
{
delete[] p[m];
}
delete[] p;
tempP[r+1] = new OriData[1];
p = tempP;
tempP = NULL;
r++;
p[r][s[r]] = p[r][s[r]] + *(oridataP + i);
}
}