Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting bad_array_new_length error when trying to load a text file into a dynamically allocated 2d array

So the issue I'm having with this code is that when I run it, I get the error: terminate called after throwing an instance of "std:bad_array_new_length" what(): std:: bad_array_new_length There are no errors otherwise and the code compiles just fine. Below is the text file I'm trying to load:

10  8
0   255 255 255 0   0   255 255 255 0
255 0   255 255 0   0   255 255 0   255
255 255 0   255 255 255 255 0   255 255
255 255 255 0   255 255 0   255 255 255
255 255 255 355 0   0   255 255 255 255
255 255 255 255 0   0   255 255 255 255
255 255 255 0   255 255 0   255 255 255
0   0   0   255 255 255 255 0   0   0

The first two values (10/8) are the length(rows) and height(columns) and the rest are meant to be stored in the 2d array.

#include <iostream>
#include <fstream>
#include <string>
#include "image.h" // Has the prototypes for the functions

using namespace std;

int** load(string imageFile, int &length, int &height) {
    int** array = new int*[length];
    ifstream file(imageFile);
    if(file.is_open()) {
        file >> length; // Takes the first value and stores it as length
        file >> height; // Takes the second value and stores it as height
        for(int i = 0; i < length; i++) {
            array[i] = new int[height]; 
            for(int j = 0; j < height; j++) {
                file >> array[i][j];
            }
        }
    }
    else {
        cout << "Unable to open file." << endl;
        return 0;
    }
    return array;
}

int main() {
    int height, length;
    int **image = load("../resource/imagecorrupted.txt", length, height);
}

I should add that this is for a homework assignment, so I'm therefor pretty restricted in the things I can do. In main I do need to store the values from load into **image, as well as the function parameters being set out for me, and are unchangeable. Also, it's still pretty early on in the class so for this assignment we aren't expected to use structs or classes or anything like that. Thanks for any help/advice you can give me!

EDIT:

Thanks to @IgorTandetnik talking me through my issue, I found the fix. This new code works, should anyone have the same issue in the future and needs help:

int** load(string imageFile, int &length, int &height) {
    ifstream file(imageFile);
    if(file.is_open()) {
        file >> length; 
        int** array = new int*[length];
        file >> height;
        for(int i = 0; i < length; i++) {
            array[i] = new int[height]; 
            for(int j = 0; j < height; j++) {
                file >> array[i][j];
            }
         }
         return array;
    }
    else {
        cout << "Unable to open file." << endl;
        return 0;
    }
}

int main() {
    int height = 0;
    int length = 0;
    int **image = load("../resource/imagecorrupted.txt", length, height);
}
like image 847
Spago Avatar asked Oct 15 '25 07:10

Spago


1 Answers

int** array = new int*[length] At this point, length is an uninitialized variable. Your program exhibits undefined behavior.

Practically speaking, length probably holds a very large garbage value. Attempting to allocate a chunk of memory this large then fails.

like image 97
Igor Tandetnik Avatar answered Oct 16 '25 21:10

Igor Tandetnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!