4

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);
}
Spago
  • 69
  • 1
  • 1
  • 6
  • You should also become familiar how to use a debugger. The problem would be very apparent if you stepped through the code with a debugger. – Eljay Feb 19 '19 at 00:57
  • *we aren't expected to use structs or classes* -- You're using `std::string` and `std::ifstream`. – PaulMcKenzie Feb 19 '19 at 01:03

1 Answers1

4

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.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • Yep. Thank you @IgorTandetnik for your response. Not sure how I missed that. Now however, I'm getting a `double free or corruption` error. I'm not sure if you know what could be causing that, but regardless, I appreciate your answer. – Spago Feb 19 '19 at 01:31
  • What has changed between "then" and "now"? My crystal ball is cloudy lately. – Igor Tandetnik Feb 19 '19 at 01:35
  • Sorry, I really should have said what I've done. All I did was initialize `length` and `height` in `main` to 0, as they get their actual values inside the `load` function. Hopefully I didn't misinterpret the fix you gave me, although doing this did fix the initial error I had. – Spago Feb 19 '19 at 01:54
  • Well then. `int** array = new int*[length]` allocates an array of length 0, then `array[i]` exhibits undefined behavior by way of accessing an index out of bounds. – Igor Tandetnik Feb 19 '19 at 02:01
  • Ah. It seems that I misunderstood the initial answer you gave me... I'll do some testing and get back to you. Thanks for your help! – Spago Feb 19 '19 at 02:51