A.Pissicat
A.Pissicat

Reputation: 3285

C++ best way to save large array

I want to create a class so save images.

class MyImage
{
public :
    int height;
    int width;
    int size;
    int matrix[];

    MyImage(int h, int w)
    {
        this->height = h;
        this->width = w;
        this->size = h * w;
        this->matrix = new int[size];
    }
}

But I got this error :

field has incomplete type 'int []'

I've try to use vector but when I want to browse each pixel in an image 700x700, it take several time.

I've seen something about the template<size_t> but I don't know how to use it.

Or maybe using int * is better but I'll have manage memory by myself isn't it ?

Is it possible to create a int[0] and resize it in the constructor ?

What is the best way ?

Upvotes: 1

Views: 814

Answers (2)

Slava
Slava

Reputation: 44268

What is the best way ?

In your case if you know size at compile time, use std::array, if you do not use std::vector and initialize it properly. Creating memory manually would not give you anything, only headaches, std::vector does the same under the hood.

Upvotes: 7

user2672107
user2672107

Reputation:

It's C++, use std::vector

#include <vector>
class MyImage
{
public :
    int height;
    int width;
    std::vector<int> matrix;

    MyImage(int h, int w) : height(h), witdh(w), matrix(h*w)
    {
    }
}

Upvotes: 4

Related Questions