声明新的int [n]时std :: bad_alloc

时间:2018-04-03 20:33:44

标签: c++ linux g++ bad-alloc

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

该计划适用于

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上的Codeblocks以及使用g ++的Linux上都会发生这种情况。

整个代码如下:

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

非常感谢所有帮助!

3 个答案:

答案 0 :(得分:3)

int numPos; int numNeg; 

由于您尚未为这些值分配任何内容(或对其进行初始化),因此它们默认初始化为不确定的值read more here。可能发生的事情是它们非常大而且太大而new无法分配空间。

首先将它们都设为0:

int numPos = 0;
int numNeg = 0;

虽然count将它们设置为0会更正确,但因为函数的后置条件是它们等于看到的正数和负数,而不是增加了许多次

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

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:0)

您需要将numNegnumPos初始化为0