确切地说。我在一个函数中创建了一个动态数组,然后将指针指向另一个函数,以便在末尾添加新元素并尝试删除[]。首先,它给了我一个未加载的wntdll.pdb,我迅速下载了该文件。然后,它只会在删除时导致异常。我认为这与保护有关,但是我不知道如何规避它。
有什么想法吗?
顺便说一句,它在第32行加载了一个“ delete_scalar.cpp”文件
_free_dbg(block, _UNKNOWN_BLOCK);
我的代码(简短版本):
int addNode(long *oldArray, int size) {
long *newArray = new long[size + 1];
long *bufArray = oldArray;
for (int i = 0; i < size; i++) {
newArray[i] = oldArray[i];
}
oldArray = newArray;
delete[] bufArray; //the problem line
size++;
return size;
}
我的代码(完整的版本,是的,所有过时的解决方案都是课程的一部分,并且是强制性的):
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void convertToBin(string const nameIn, string const nameOut);
void readFile(string const fileName);
long *getNewArr(long **arrMatrix, int *arrSizes, int arrNum);
int addNode(long *oldArray, int size);
void outputArr(long *resArr);
void main() {
setlocale(LC_ALL, "");
string preinput = "input.txt", input = "input.bin", output = "output.txt";
convertToBin(preinput, input);
readFile(input);
cout << "Готово!\n"; //Done!
}
//this is where the array is created
void readFile(string fileName) {
cout << "Чтение файла\n"; //reading the file
ifstream streamIn;
streamIn.open(fileName, ios::in | ios::binary);
if (!streamIn.is_open()) {
cout << "Ошибка чтения файла\n"; //file reading error
return;
}
cout << "Получение размеров массивов\n"; //get array sizes
int arrNum;
streamIn.read((char*)&arrNum, sizeof(int));
int *arrSize = new int[arrNum];
for (int i = 0; i < arrNum; i++)
{
streamIn.read((char*)&arrSize[i], sizeof(int));
}
cout << "Создание первоначальных массивов\n"; //setup initial arrays
long **arrMatrix = new long *[arrNum];
for (int i = 0; i < arrNum; i++)
{
arrMatrix[i] = new long[arrSize[i]];
}
for (int i = 0; i < arrNum; i++)
for (int j = 0; j < arrSize[i]; j++) {
streamIn.read((char*)&arrMatrix[i][j], sizeof(long));
}
streamIn.close();
outputArr(getNewArr(arrMatrix, arrSize, arrNum));
cout << "Очистка памяти\n"; //memory cleanup
for (int i = 0; i < arrNum; i++)
delete[] arrMatrix[i];
delete[] arrMatrix;
delete[] arrSize;
}
long *getNewArr(long **arrMatrix, int *arrSizes, int arrNum) {
cout << "Создание нового массива\n"; //creating new array
long *newArr = new long [0];
int newArrSize = 0;
for (int i = 0; i < arrSizes[0]; i++) { //the heart of the
long x = arrMatrix[0][i]; //program. multiplies
bool isCommon = 1; //x sets, outputs the
for (int j = 1; j < arrNum; j++) { //result preceeded by
isCommon = 0; //size.
for (int k = 0; k < arrSizes[j]; k++)
if (x == arrMatrix[j][k]) { isCommon = 1; break; }
}
if (isCommon) {
newArrSize = addNode(newArr, newArrSize);
newArr[newArrSize - 1] = x;
}
}
addNode(newArr, newArrSize);
for (int i = 0; i < newArrSize; i++)
newArr[i + 1] = newArr[i];
newArr[0] = newArrSize;
return newArr;
}
int addNode(long *oldArray, int size) { //the problem function
long *newArray = new long[size + 1];
long *bufArray = oldArray;
for (int i = 0; i < size; i++) {
newArray[i] = oldArray[i];
}
oldArray = newArray;
delete[] bufArray; //the problem line
size++;
return size;
}
void outputArr(long *resArr) {
cout << "Сохранение нового массива\n"; //saving new array
ofstream streamOut;
streamOut.open("output.txt");
if (!streamOut.is_open()) {
cout << "Ошибка чтения файла\n"; //file read error
return;
}
for (int i = 0; i <= resArr[0]; i++) {
streamOut << resArr[i] << " ";
}
streamOut.close();
delete[] resArr;
}
void convertToBin(string const nameIn, string const nameOut)
{ //he has us bloody convert
ifstream streamIn; //to binary before doing
streamIn.open(nameIn, ios_base::in); //anything.
ofstream streamOut;
streamOut.open(nameOut, ios_base::out | ios_base::binary);
if (!(streamIn.is_open() && streamOut.is_open()))
{
cout << "Ошибка чтения файлов!\n";
return;
}
int x;
while (!streamIn.eof()) {
streamIn >> x;
streamOut.write((char*)&x, sizeof(int));
}
streamIn.close(); streamOut.close();
}