一个关于动态数组的莫名其妙的内存越界的有关问题
一个关于动态数组的莫名其妙的内存越界的问题
程序如下。输入第11个数字、也就是正要发生第二次申请动态内存的时候报错。可是研究了半天代码,也不知道为何会出错。
------解决思路----------------------
超过了动态数组定义的长度。很多情况下,在预编译过程阶段,数组的长度是不能预先知道的,必须在程序运行时动态的给出
但是问题是,c++要求定义数组时,必须明确给定数组的大小,要不然编译通不过,所以 你看看你所定义的数组长度是不是不够,定义大一点试试
------解决思路----------------------
数组越界,因为你是动态申请,所以 在heap上,你申请大一点看看有没有问题
------解决思路----------------------
你count刚超过maxCount的时候,pIntArr只分配了count=maxCount+1,但是maxCount后面却+了5,这样再往后加的4个东西全是越界的。
------解决思路----------------------
malloc,realloc,free
程序如下。输入第11个数字、也就是正要发生第二次申请动态内存的时候报错。可是研究了半天代码,也不知道为何会出错。
//
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
int main( )
{
int begin[]{1, 1, 2, 3, 5};
int* pIntArr = nullptr;
size_t count{_countof(begin)}; // The number of valid elements in the array.
size_t maxCount(count * 2); // The dimension of the dynamic array.
pIntArr = new int[maxCount]{};
int number{};
int sum{};
/*
First, copying the elements of begin to pIntArr.
*/
for (size_t i = 0; i < count; ++i)
{
pIntArr[i] = begin[i];
sum += begin[i];
}
cout << "First examine these " << _countof(begin) << " numbers, try to "
<< "find the pattern, and write as many subsequent numbers as youc can."
<< "If you get stuck, type in -1 to terminate.\n\n";
for (size_t i = 0; i < count; ++i)
{
cout << pIntArr[i] << ' ';
}
cout << endl << endl;
cin >> number;
while (number != -1)
{
++count;
sum += number;
// Then we need to update maxCount, and more importantly,
// we need to reallocate memory space for pIntArr.
if (count > maxCount)
{
cout << "Now count == " << count
<< ", so it's time to update the maxCount!" << endl;
int* pIntTemp = new int[maxCount]{};
// First, we copy pIntArr into pIntTemp.
for (size_t i = 0; i < maxCount; ++i)
{
pIntTemp[i] = pIntArr[i];
}
// Then we reallocate pIntArr...
if (pIntArr)
{
delete[] pIntArr;
pIntArr = nullptr;
cout << "pIntArr has been deallocated." << endl;
}
pIntArr = new int[count]{-1};
// ...and copy pIntTemp into pIntArr
for (size_t i = 0; i < maxCount; ++i)
{
pIntArr[i] = pIntTemp[i];
}
// ...and don't forget to add the newly-input
// number!!!
// pIntArr[count - 1] = number;
pIntArr[maxCount] = number;
// Now that pIntTemp has served its purpose,
// just destroy it!
if (pIntTemp)
{
delete[] pIntTemp;
pIntTemp = nullptr;
}
cout << "pIntTemp has been deallocated." << endl;
// maxCount += _countof(begin);
maxCount += _countof(begin);
cout << "maxCount has been amplified. maxCount == " << maxCount << endl;
}
else // Then just fill in the next blank.
{
pIntArr[count - 1] = number;
cout << "normal assignment." << endl;
}
cin >> number;
}
cout << "So, you have entered:\n";
for (size_t i = 0; i < count; ++i)
{
if (i % 5 == 0)
cout << endl;
cout << setw(10) << pIntArr[i];
}
cout << endl;
std::cout << "The " << count << " numbers' average is:\t"
<< static_cast<double>(sum) / static_cast<double>(count) << endl;
delete[] pIntArr;
pIntArr = nullptr;
return 0;
}
------解决思路----------------------
超过了动态数组定义的长度。很多情况下,在预编译过程阶段,数组的长度是不能预先知道的,必须在程序运行时动态的给出
但是问题是,c++要求定义数组时,必须明确给定数组的大小,要不然编译通不过,所以 你看看你所定义的数组长度是不是不够,定义大一点试试
------解决思路----------------------
数组越界,因为你是动态申请,所以 在heap上,你申请大一点看看有没有问题
------解决思路----------------------
你count刚超过maxCount的时候,pIntArr只分配了count=maxCount+1,但是maxCount后面却+了5,这样再往后加的4个东西全是越界的。
------解决思路----------------------
malloc,realloc,free