free():C ++中的指针错误无效

时间:2017-10-08 04:03:22

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

Heyo,所以当我在linux服务器上运行/编译我的程序时,我出现了这个错误消息。老实说,我不知道这是什么bug。我有大约一个小时来修复我的代码,因此了解我需要纠正的内容真是太棒了。谢谢!

这是我目前的代码:

    #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;
 }

1 个答案:

答案 0 :(得分:2)

由于您未从numbers获得new[],因此无法将其传递给delete[]

顺便说一下,int numbers[arraySize]; arraySize在编译时不知道docker inspect <container id> | grep -i ip 是无效的标准C ++,它是编译器扩展。所以要小心它做了什么,不能保证。