【问题标题】:std::bad_alloc when declaring new int[n] [closed]声明新 int[n] 时的 std::bad_alloc [关闭]
【发布时间】:2018-04-03 20:33:44
【问题描述】:

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

该程序的工作原理包括

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

非常感谢所有帮助!

【问题讨论】:

  • 您使用的n 有多大?分配如此大量的连续内存可能是不可能的
  • 在使用前我没有看到numNeg的任何初始化。

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


【解决方案1】:
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];
}

【讨论】:

    【解决方案2】:

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

    我将其编译并调试为工作版本。 当您想解决此类问题时,调试器是您的朋友;并使用手表查看变量和数组的内容来检查它。 这里是更正的代码。玩得开心。

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

    【讨论】:

    • 这是不对的。您的函数应在开始计数之前将其参数设置为 0
    • @Ryan,我只是使用了请求者的代码并更正了它以显示需要更改的内容(如解释的那样),这没有错,但我同意将变量设置为零计数和拆分功能更好。我在上面的代码中相应地更改了它。谢谢。
    【解决方案3】:

    您需要将numNegnumPos 初始化为0

    【讨论】:

    • 我现在真的很傻!这太快了,我什至不能再接受 7 分钟
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 2022-09-16
    • 2016-12-30
    • 1970-01-01
    相关资源
    最近更新 更多