0

I only found solutions for 1d arrays, but couldn't apply them to 2d arrays. The possible solutions included "vectors", "templates", and "pointers to arrays".

I know I can get vectors to work, but I would rather use either of the other 2. Preferably templates because I don't want to manually destruct, but pointers work too. (the pointer would be pointed to an array created in the constructor).

The class contains an empty 2d array called screen. The constructor is supposed to set its size. I tried too many things for me to list them all here, but I'll show what I currently have. (last thing i tried were pointers to arrays created in the constructor. in this case screen was a char pointer)

Screen::Screen(const int w, const int h) : screen(new char[h][w]) {} {
    width = w;
    height = h;
}

array size in new-expression must be constant

I failed implementing either of those strategies and received many kinds of errors while trying to make it work. How would I solve this problem? (primarily I want to know how to do this with templates. if not possible then with pointers to arrays created in the constructor)

6
  • 1
    How are vectors, templates and pointer to array connected... Anyway, I don't understand your question. Post some actual code that you've tried writing and we'll see what's wrong with it. Commented Mar 8, 2017 at 0:02
  • Add some code first, show us what you tried. Commented Mar 8, 2017 at 0:15
  • Are the w, h values all known at compile-time? Commented Mar 8, 2017 at 0:40
  • Using a 1-D array with a 2-D indexing operator may be your best solution Commented Mar 8, 2017 at 0:41
  • the values are not known at compile time (probably), but do not need to be changed once the class has been created. (which is why i dont really want to use vectors as I don't plan on expanding the array, and only need array functionality) Commented Mar 8, 2017 at 0:46

1 Answer 1

1

The question was a little ambiguous, but it sounds like you want to dynamically allocate an array given some input. Edit I changed the answer to match the code you provided. This creates a 2d array of chars given the height and width.

class Screen {
    private:
        char **data;
        int rows;
        int columns;

    public:
        Screen(int num_rows, int num_cols); 
}; 

Screen::Screen(int num_rows, int num_cols) {
    data = new char * [num_rows];
    for (int i = 0; i < num_rows; ++i) {
        data[i] = new char[num_cols];
    }
    rows = num_rows;
    columns = num_cols;
}

This creates an empty 2D array of chars.

Explanation: All arrays in c are just pointers to the first block in memory of the type you have declared. By having the member variable as double pointer, you have an array of char pointers, which each point to the first value in each of their respective arrays.

BUT be careful, you WILL need to free the data variable to avoid memory leaks, by declaring a destructor.

Screen::~Screen() {
    for (int i = 0; i < rows; ++i) {
        delete[] data[i];
    }
    delete[] data;
}
Sign up to request clarification or add additional context in comments.

3 Comments

You forgot to free something in your destructor. delete[] data
This is a lot of mucking around and violates the rule of three - you should use a vector or a vector of vectors instead of this approach
I agree a vector of vectors would be far easier, but he did ask how to do this with either a template or pointer to arrays in the constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.