指针、动态数组和内存泄漏
Pointers, dynamic arrays, and memory leak
我正在尝试创建一个程序,允许动态分配的数组存储一些整数,如果需要增加最大大小,然后按该顺序显示未排序和排序的数组。
Link我的完整代码在底部。
我遇到的第一个问题是动态分配的数组在第一次需要增加大小后变得混乱。相关代码如下。
while (counter <= arraySize)
{
cout <<"Please enter an integer number. Use 999999 (six 9's) to stop\n";
if (counter == arraySize) //If the counter is equal to the size of the array
{ //the array must be resized
arraySize +=2;
int *temp = new int[arraySize];
for (counter = 0; counter < arraySize; counter++)
{
temp[counter] = arrayPtr[counter];
}
delete [] arrayPtr;
arrayPtr = temp;
counter ++; //the counter has to be reset to it's original position
} //which should be +1 of the end of the old array
cin >> arrayPtr[counter];
if (arrayPtr[counter] == sentinel)
{
cout << "Sentinel Value given, data entry ending.\n";
break;
}
counter ++;
}
这会产生意外操作,它不会等待标记值,而是开始列出内存中超过该点的整数(因为没有边界检查)。
下一个问题是我的排序函数拒绝 运行。我尝试在 5 个值上测试它,程序在到达代码的特定部分时崩溃。
函数调用使用
sorting (arrayPtr);
但函数本身看起来像这样:
void sorting (int *arr)
{
int count = 0, countTwo = 0, tempVal;
for (count = 0; arr[count] != 999999; count++) //I figured arr[count] != 999999 is easier and looks better
{ //A bunch of if statements
for (countTwo = 0; arr[countTwo] != 99999; countTwo++)
{
if (arr[countTwo] > arr[countTwo+1])
{
tempVal = arr[countTwo];
arr[countTwo] = arr[countTwo+1];
arr[countTwo+1] = tempVal;
}
}
}
}
感谢任何有关此问题的帮助。
Link到我的源代码:
http://www.mediafire.com/file/w08su2hap57fkwo/Lab1_2336.cpp
由于社区反馈,此 link 将尽可能保持活跃 。
下面的link是我修改后的源代码。它被注释是为了更好地突出我所犯的错误以及修复它们的答案。
http://www.mediafire.com/file/1z7hd4w8smnwn29/Lab1_2336_corrected.cpp
我在您的代码中发现的第一个问题是在 for 循环中,其中计数器从 0 变为 arraySize-1,循环的最后两次迭代将越界访问 arrrayPtr。
接下来,在 if (counter == arraySize)
的末尾有一个 counter++;
这不是必需的,因为此时 counter
已经超出范围索引数组。
最后,在您的排序函数中,内部循环查找了错误的值(99999 而不是 999999),因此它永远不会停止并超出范围。为防止此类错误,您应该将 sentinel
定义为未命名命名空间中的常量,并通过代码使用它,而不是键入 999999(这很容易出错...)。
我正在尝试创建一个程序,允许动态分配的数组存储一些整数,如果需要增加最大大小,然后按该顺序显示未排序和排序的数组。
Link我的完整代码在底部。
我遇到的第一个问题是动态分配的数组在第一次需要增加大小后变得混乱。相关代码如下。
while (counter <= arraySize)
{
cout <<"Please enter an integer number. Use 999999 (six 9's) to stop\n";
if (counter == arraySize) //If the counter is equal to the size of the array
{ //the array must be resized
arraySize +=2;
int *temp = new int[arraySize];
for (counter = 0; counter < arraySize; counter++)
{
temp[counter] = arrayPtr[counter];
}
delete [] arrayPtr;
arrayPtr = temp;
counter ++; //the counter has to be reset to it's original position
} //which should be +1 of the end of the old array
cin >> arrayPtr[counter];
if (arrayPtr[counter] == sentinel)
{
cout << "Sentinel Value given, data entry ending.\n";
break;
}
counter ++;
}
这会产生意外操作,它不会等待标记值,而是开始列出内存中超过该点的整数(因为没有边界检查)。
下一个问题是我的排序函数拒绝 运行。我尝试在 5 个值上测试它,程序在到达代码的特定部分时崩溃。
函数调用使用
sorting (arrayPtr);
但函数本身看起来像这样:
void sorting (int *arr)
{
int count = 0, countTwo = 0, tempVal;
for (count = 0; arr[count] != 999999; count++) //I figured arr[count] != 999999 is easier and looks better
{ //A bunch of if statements
for (countTwo = 0; arr[countTwo] != 99999; countTwo++)
{
if (arr[countTwo] > arr[countTwo+1])
{
tempVal = arr[countTwo];
arr[countTwo] = arr[countTwo+1];
arr[countTwo+1] = tempVal;
}
}
}
}
感谢任何有关此问题的帮助。
Link到我的源代码:
http://www.mediafire.com/file/w08su2hap57fkwo/Lab1_2336.cpp
由于社区反馈,此 link 将尽可能保持活跃 。
下面的link是我修改后的源代码。它被注释是为了更好地突出我所犯的错误以及修复它们的答案。
http://www.mediafire.com/file/1z7hd4w8smnwn29/Lab1_2336_corrected.cpp
我在您的代码中发现的第一个问题是在 for 循环中,其中计数器从 0 变为 arraySize-1,循环的最后两次迭代将越界访问 arrrayPtr。
接下来,在 if (counter == arraySize)
的末尾有一个 counter++;
这不是必需的,因为此时 counter
已经超出范围索引数组。
最后,在您的排序函数中,内部循环查找了错误的值(99999 而不是 999999),因此它永远不会停止并超出范围。为防止此类错误,您应该将 sentinel
定义为未命名命名空间中的常量,并通过代码使用它,而不是键入 999999(这很容易出错...)。