如何在c++中删除新数组

How to delete the new array in c++?

本文关键字:数组 新数组 删除 c++      更新时间:2024-04-28

我在c++中创建了一个新的点函数,在这里我通过动态内存分配来创建一个新数组。然而,当我想删除主函数中的数组时,它会报告错误,如:

'Assessment_1.exe' (Win32): Unloaded 'C:WindowsSysWOW64ucrtbased.dll'
'Assessment_1.exe' (Win32): Loaded 'C:WindowsSysWOW64ucrtbased.dll'. 
The thread 0x22dc has exited with code 0 (0x0).
HEAP[Assessment_1.exe]: Invalid address specified to RtlValidateHeap( 00050000, 0005F25C )
Assessment_1.exe has triggered a breakpoint.

return_array.cpp包含用户在其中输入键盘的函数。

#include<iostream>
#include <conio.h>
using namespace std;
//const static int length = 3;
int* input() {
//static int number[length];  fixed length for input 
static int* number_array;
int number_length = 30;
int index = 0;
number_array = new int[number_length];
cout << "enter (ESC) to escape the program!" << endl;
while (_getch() != 27)
{
cout << "Input your number " << index << " elment: " << endl;
cin >> number_array[index];
index = index + 1;
cout << endl;
}
return number_array;
}

practice.cpp包含两个向量的主函数和求值函数。错误是当我添加delete[]vector_1

#include <vector>
#include<iostream>
#include"myFunctions.h"
using namespace std;
const static int length = 3;
bool same_vec(vector<int> a, vector<int> b);
void main() {
vector<int> new_vector_1(length);
vector<int> new_vector_2(length);
int* vector_1 = input();
for (int i = 0; i < length; i++) {
new_vector_1[i] = *vector_1;
cout << *vector_1 << endl;
vector_1 = vector_1 + 1;
}
delete []vector_1;
for (int i = 0; i < length; i++) {
cout << " The result of vector_1: " << new_vector_1[i] << endl;
}
int* vector_2;
vector_2 = input();
for (int i = 0; i < length; i++) {
new_vector_2[i] = *vector_2;
vector_2 = vector_2 + 1;
}
delete []vector_2;
for (int i = 0; i < length; i++) {
cout << " The result of vector_2: " << new_vector_2[i] << endl;
}
bool qax = same_vec(new_vector_1, new_vector_2);
if (qax == false) {
cout << "the items are not match!"<<endl;
}
else {
cout << "the items are match!" << endl;
}
}
bool same_vec(vector<int> a, vector<int> b) {
//Evaluate the elements in the two vectors
bool flag = true;
int length_a = a.size();
int length_b = b.size();
vector<bool> new_bool(length_a);
for (int i = 0; i < length_a; i++) {
for (int j = 0; j < length_b; j++) {
if (a[i] == b[j]) {
new_bool[i] = true;
cout << a[i] << " " << b[j] << endl;
break;
}
}
}
for (int i = 0; i < new_bool.size(); i++) {
if (new_bool[i] == false) {
flag = false;
}
}
return flag;
}

而myFunction.h是我的头文件。

#pragma once
int* getRandom();
int* input();

有人能帮忙解决这个问题吗?我知道一个解决方案是删除delete[]vector_1的行。该解决方案可能会导致内存泄漏。

您正在更改存储在vector_1指针中的地址,然后尝试delete[]某个input函数中new运算符返回的指针不再对应的东西

所以,代替这个循环:

for (int i = 0; i < length; i++) {
new_vector_1[i] = *vector_1;
cout << *vector_1 << endl;
vector_1 = vector_1 + 1; // This line changes the pointer!
}
delete []vector_1; // And, here, the pointer is NOT what "new" gave you!

使用类似的东西,而不是:

for (int i = 0; i < length; i++) {
new_vector_1[i] = vector_1[i]; // You can use the [] operator on the pointer
cout << *vector_1 << endl;
//      vector_1 = vector_1 + 1; // Remove this line, as it's causing the problem!
}
delete []vector_1;

此外,在处理vector_2指针的循环中也存在完全相同的问题,同样的"修复"也会在那里起作用。

注意:如果您不想使用[i]索引运算符,而是使用指针"算术",那么您可以更改:

new_vector_1[i] = vector_1[i]; 

至:

new_vector_1[i] = *(vector_1 + i);

通过这种方式,您将i的值添加到指针,而不必更改该指针。