c++ - free(): invalid pointer error in C++

标签 c++ debugging pointers visual-c++ compiler-errors

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/

相关文章:

c++ - 调试通过 LoadLibrary() 加载到 Excel 中的 C++ DLL

android - 0 个进程和 1 个服务

java - 运行时切换运行配置

c - 关于在 C 中使用指针定义二维数组

c - Turbo C 中的 int 指针不应该是 4 个字节吗?

c++ - 段错误 : C++ sort an string vector with lambda comparator

c++ - 四边形输出纹理不正确

c++ - 我们可以发送一对模板参数给另一对吗

c - 在 C 中读取二进制文件的奇怪之处

c++ - 为什么在从控制台编译后取消引用 string::iterator 不会在 .end() 处引发错误?