如何在c++中删除新数组
How to delete the new array in c++?
我在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
的值添加到指针,而不必更改该指针。
相关文章:
- 如何将元素从向量转移到新数组?
- 如何在新数组较小时创建新数组并将旧数组的最后一个元素复制到新数组中?
- C++ "oldstyle container ":指针/数组/新 - 可能误会?
- 是复制了新数组还是指向结构中引用的数组的指针?
- 标量"新 T "与数组"新 T[1]"
- 如何将大括号初始值设定项用于动态大小数组和新数组?
- 如何按姓氏排序并打印新数组
- 如何修改数组,从中删除空格,然后将其存储在新数组中
- 有没有办法创建一个花哨的迭代器和相应的新数组,以便检查每个索引的索引值的条件?
- 从一系列的evens和赔率中,新数组是持续的第一个赔率
- 在新数组中获取随机值
- 将数组的特定元素移动到新数组
- 我如何使用此二叉搜索函数的修改后的最大/最小值创建新数组
- 如何从函数返回的数组中创建一个新数组
- 合并排序 - 返回新数组,而不是将合并的数组复制到输入数组
- C 2 DIM数组新获得0
- 创建新数组时写入位置时发生访问冲突
- 将 argv 复制到新数组中
- C++中的动态数组(指针数组还是新数组?
- 编译器认为新数组正在创建新对象