Heyo,所以当我在Linux服务器上运行/编译程序时,出现了此错误消息。老实说,我不知道错误是什么。我有大约一个小时的时间来修复我的代码,因此对我需要纠正的问题有个好主意。谢谢!
这是我当前的代码:
#include <iostream>
#include "Car.h"
using namespace std;
void problemOne(int arraySize) {
int numbers[arraySize];
cout << "Enter next number" << endl;
for (int i = 0; i < arraySize; ++i) {
cin >> numbers[i];
}
int *p = numbers;
for (int i = 0; i < arraySize; i++) {
cout << *p;
p++;
}
}
for (int i = 0; i < arraySize; i++) {
cout << "Enter next number" << endl;
cin >> *numbers;
numbers++;
}
for (int i = 0; i < arraySize; i++) {
cout << *numbers;
numbers++;
}
delete[] numbers;
}
Car *p = new Car[arraySize];
for (int i = 0; i < arraySize; i++) {
string color;
string model;
int mile;
cout << "enter the model of the car" << " ";
cin >> model;
cout << "enter color of the car" << " ";
cin >> color;
cout << "enter the mileage of the car" << " ";
cin >> mile;
*p = Car(model, color, mile);
}
for (int i = 0; i < arraySize; i++) {
cout << p->getModel() << " " << p->getColor() << " " << p->getMileage() << endl;
p++;
}
}
int main(int argc, char* argv[]) {
problemOne(atoi(argv[1]));
problemTwo(atoi(argv[2]));
problemThree(atoi(argv[3]));
return 0;
}
最佳答案
由于您没有从numbers
获得new[]
,因此无法将其传递给delete[]
。
顺便说一下,在编译时不知道int numbers[arraySize];
的arraySize
是无效的Standard C++,它是编译器扩展。因此,请注意它的作用和不能保证的作用。
关于c++ - free(): invalid pointer error in C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46627388/