std::bad_alloc 声明 new int[n] 时

std::bad_alloc when declaring new int[n]

该程序应该从用户输入中获取一个数组,并将其分成两个数组,分别用于负值和非负值。

程序运行到并包括

count(userList, n, numPos, numNeg); 

声明时出现错误

int *negList = new int[numNeg];
int *posList = new int[numPos];

我认为将其更改为

int *negList;
negList = new int[numNeg];
int *posList;
posList = new int[numPos];

会解决问题,但事实并非如此。

的先前声明
int *userList;
userList = new int[n];

不会抛出任何错误。

这在 windows 和 Linux 上的 Codeblocks 中都会发生。

完整代码如下:

#include <iostream>

using namespace std;

//count positive and negative elements in list
void count(const int* arr/*list*/, int numElements/*num elements in array*/, int& numPos/*num positive elements*/, int& numNeg/*num negative elements*/);

int main()
{
  //declare variables
  int n;                  //number of elements
  int userInput;          //place holder for list values
  int numPos; int numNeg; //num positive and negative elements

  //prompt user for number of elements
  cout << "Enter number of elements: ";
  cin >> n;

  //declare array
  int *userList;
  userList = new int[n];

  //prompt user for list and read in
  cout << "Enter list: " << endl;
  cin >> userInput;
  for(int i(0); i < n; i++){
    cin >> userInput;
  }

  //count positive and negative elements
  count(userList, n, numPos, numNeg);

  //declare arrays for negative and positive elements respectively
  int *negList = new int[numNeg];
  int *posList = new int[numPos];

  // ...

  //free memory
  delete [] userList;
  delete [] negList;
  delete [] posList;

  return 0;
}

void count(const int* arr, int numElements, int& numPos, int& numNeg)
{
  for(int i(0); i < numElements; i++){
    if(arr[i] < 0){
      numNeg++;
    }
    else{
      numPos++;
    }
  }
}

非常感谢所有帮助!

您需要将 numNegnumPos 初始化为 0

int numPos; int numNeg; 

由于您没有为这些值分配任何东西(或初始化它们),它们默认初始化为不确定值 read more here。可能发生的情况是它们非常大,new 无法分配 space。

首先将它们都设置为 0:

int numPos = 0;
int numNeg = 0;

虽然 count 将它们设置为 0 会更正确,因为函数的 post-condition 是它们等于看到的正负数,而不是递增了那么多次

void count(const int* arr, int numElements, int& numPos, int& numNeg {
    numPos = numNeg = 0;
    // ...
}

另一个错误是您从未填充 userList 而是继续读入同一个 userInput 变量,您的初始 for 循环应该是

//prompt user for list and read in
cout << "Enter list: " << endl;
for(int i(0); i < n; i++){
  cin >> userList[i];
}

如上所述,将变量初始化为零并通过引用传递它们(它的地址,而不是它的值)将解决您遇到的问题。

我编译并调试它到一个工作版本。 当你想解决这类问题时,调试器是你的朋友;并使用 watches 查看变量和数组的内容来检查它。 这里是更正后的代码。玩得开心。

#include <iostream>

using namespace std;

//count positive and negative elements in list
void count( const int* arr          /*list*/, 
        int numElements         /*num elements in array*/, 
        int& numPos             /*num positive elements*/, 
        int& numNeg             /*num negative elements*/);

//split list into list of positive elements and list of negative elements
void split( const int* original     /*original list*/, 
        const int numOrig       /*size of original list*/, 
        int* negList            /*negative list*/, 
        int& numNeg             /*size of negative list*/, 
        int* posList            /*positive list*/, 
        int& numPos             /*size of positive list*/);

//prints array
void print_array(const int* arr, const int arrSize);

int main()
{ 
    //declare variables
    int n;                  //number of elements
    int userInput;          //place holder for list values

//prompt user for number of elements
    cout << "Enter number of elements: ";
    cin >> n;

    //declare array
    int* userList = NULL;
    userList = new int[n];

    //prompt user for list and read in
    cout << "Enter list: " << endl;

    for (int i=0; i < n; i++) {
        cin >> userInput;
        userList[i] = userInput;    
    }

    //count positive and negative elements
    count(userList, n, numPos, numNeg);

    //declare arrays for negative and positive elements respectively
    int* negList = new int[numNeg];
    int* posList = new int[numPos];

    //split array into positive and negative arrays
    split(userList, n, negList, numNeg, posList, numPos);

    //print arrays
    cout << "Negative elements: " << endl;
    print_array(negList, numNeg);
    cout << "Non-negative elements: " << endl;
    print_array(posList, numPos);

    //free memory
    delete[] userList;
    delete[] negList;
    delete[] posList;

    cin >> userInput;  // added to show result in console window (can be removed)

    return 0;
}

void count(const int* arr, int numElements, int& numPos, int& numNeg)
{
    numPos=0;           //num positive and negative elements
    numNeg=0 ; 

    for (int i(0); i < numElements; i++) {
        if (arr[i] < 0) {
            numNeg++;
        }
        else {
            numPos++;
        }
    }
}

void split(const int* original, const int numOrig, int* negList,  int& numNeg, int* posList,  int& numPos)
{
    numPos = 0;           //num positive and negative elements, reset to zero
    numNeg = 0;

    for (int i=0; i < numOrig; i++) {
        if (original[i] < 0) {
            negList[numNeg] = original[i];
            numNeg++;
        }
        else {
            posList[numPos] = original[i];
            numPos++;
        }
    }
}

void print_array(const int* arr, const int arrSize)
{
    for (int i=0; i < arrSize; i++) {
        cout << " " << arr[i];
    }
    cout << endl;
}