c++ - 如何使用 C++ 在结构中的堆内存中定义变量?

标签 c++

我有 2 个结构,颜色一:

struct Color
{
    unsigned char red;
    unsigned char green;
    unsigned char blue;
};

和图像结构:

struct matrixImage
{
    Image Img;
    struct Color T[MAX][MAX];
    int Width;
    int Height;
};

T 表中,我存储了图像的像素。但是数组的最大大小太低(在堆栈上),所以我无法存储图像的每个像素。我知道如何像这样在堆中定义数组

-> struct Color *newTable = new struct Color[anyNumber];

但是我怎样才能在 struct matrixImage 中写这个,有什么帮助吗?

最佳答案

我会做这样的事情:

//Switched to a class because we need some encapsulation or this will be bug-prone.
class matrixImage
{
    //Do you need this? What kind of object is "Image"?
    //Image Img;
    std::vector<Color> pixel_data;
    int Width;
    int Height;
public:
    matrixImage(int width = 1, int height = 1) :
    Width(width), Height(height), pixel_data(width * height) 
    {
    }

    //You can add bounds-checking if you need it, i.e. make sure y is less than Height, 
    //make sure x is less than Width. Not every application needs it, and you need a 
    //clear semantic of "what should happen" if you specify an invalid index.
    Color & get_color(int x, int y) {
        return pixel_data[y * Width + x];
    }
    //We have two versions to handle the case where your object is made "const"
    Color const& get_color(int x, int y) const{
        return pixel_data[y * Width + x];
    }

    //Hey, we can use the function we just defined above this one!
    void set_color(int x, int y, Color c) {
        get_color(x, y) = c;
    }

    //This only resizes the canvas. Doing proper "Resizing" is beyond the scope of what 
    //we're discussing here.
    void set_size(int new_width, int new_height) {
        std::vector<Color> new_data(new_width * new_height);

        //This case is pretty easy
        if(new_width == Width) {
            std::copy(
                pixel_data.begin(), 
                pixel_data.begin() + std::min(pixel_data.size(), new_data.size()), 
                new_data.begin()
            );
        //This gets complicated
        } else if(new_width < Width) {
            for(size_t y = 0; y < std::min(Height, new_height); y++) {
                std::copy(
                    pixel_data.begin() + y * Width,
                    pixel_data.begin() + y * Width + new_width,
                    new_data.begin() + y * new_width
                );
            }
        //Similar logic, but slightly different.
        } else {
            for(size_t y = 0; y < std::min(Height, new_height); y++) {
                std::copy(
                    pixel_data.begin() + y * Width,
                    pixel_data.begin() + (y + 1) * Width,
                    new_data.begin() + y * new_width
                );
            }
        }
        pixel_data = new_data;
        Width = new_width;
        Height = new_height;
    }

    //I leave this last one as an exercise to the reader, as it's beyond my expertise.
    void resize(int new_width, int new_height);
};

这将为您处理动态内存分配(当然还有释放),并为您提供一些基本功能,以便在您绝对需要时直接使用底层数据。

关于c++ - 如何使用 C++ 在结构中的堆内存中定义变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41148354/

相关文章:

c++ - 用于管理 C/C++ 嵌入式设备中的菜单的库

c++ - 使用 <ctime> 生成日期 vector

c++ - 使用 sockaddr_un 调用 connect(2) 时出现编译器错误

c++ - 更新具有更大值的数组元素最少需要哪些内存屏障?

c++ - 如何识别.exe文件是否需要输入参数

c++ - 使用 boost.spirit 时不推荐使用的警告

c++ - 使用类指针拆分项目

c++ - 在 C++ 中从模板创建对象

c++ - 模板类内部运算符在该类的不同特化对象之间的特化

c++ - TCP/IP 的套接字写入问题