无法为二维数组的对象变量分配 malloc

Unable to malloc for an object's variable that is a 2d array

我正在为 class 赋值,但在为对象中的 unsigned int ** 变量赋值时遇到了一些问题。出于某种原因,我不能用 malloc 分配它,说明 assignment to expression with array type 有错误。我不确定这意味着什么,我尝试直接使用 malloc 将其分配给 image->pixmap,但遇到了同样的问题。这两种方法都会发生。

P.S。由于这是一项作业,我不能更改 structs/object 定义,否则我会更改。

我会尽力以PPMImage为例来解释pixmap

pixmap: Three height x width, 2-dimensional pixel arrays, for ’R’, ‘G’, ‘B’ values
height: image height (number of rows)
width: image width (number of columns)
pixmax: maximum pixel value of image

//p->pixmap[0]: ‘R’ pixmap array
//p->pixmap[1][7]: 8th row of pixels of ‘G’ pixmap array
//p->pixmap[2][4][10]: 11th pixel in 5th row of ‘B’ pixmap array
typedef struct {
unsigned int ** pixmap[3];
unsigned int height, width, pixmax;
} PPMImage;

PPMImage * new_ppmimage( unsigned int w, unsigned int h, unsigned int m )
{

    PPMImage *image;
    image = (PPMImage *) malloc(sizeof(PPMImage));
    image -> height = h;
    image -> width = w;

    unsigned int ** tempArray = malloc(sizeof(unsigned int *)*h);
    for(int i  = 0;i < h; i++){
        tempArray[i] = malloc(sizeof(unsigned int) *w);
    }
    // issue here
    image -> pixmap = tempArray;
    return NULL;
}
=======================================
typedef struct {
unsigned int ** pixmap;
unsigned int height, width;
} PBMImage;

PBMImage * new_pbmimage( unsigned int w, unsigned int h )
{
    PBMImage *image;
    image = (PBMImage *) malloc(sizeof(PBMImage));
    image -> height = h;
    image -> width = w;
    
    // issue here
    image -> pixmap = malloc(sizeof(unsigned int *)*h);
    for(int i  = 0;i < h; i++){
        image -> pixmap[i] = malloc(sizeof(unsigned int) *w);
    }
    return NULL;
}

您的代码将永远无法正常工作 return NULL 不是 new_ppmimage 函数中分配的内存。

如果无法更改类型:

typedef struct {
unsigned int ** pixmap[3];
unsigned int height, width, pixmax;
} PPMImage;

PPMImage * new_ppmimage( unsigned int w, unsigned int h, unsigned int m )
{

    PPMImage *image;
    image = malloc(sizeof(*image));
    if(image)
    {
        image -> height = h;
        image -> width = w;
        for(size_t i = 0; i < sizeof(image -> pixmap) / sizeof(image -> pixmap[0]); i++)
        {
            image -> pixmap[i] = malloc(h * sizeof(*image -> pixmap));
            if(image -> pixmap)
            {
                for(unsigned hi = 0; hi < h; hi++)
                {
                    image -> pixmap[i][hi] = malloc(w * sizeof(**image -> pixmap[i]));
                    if(!image -> pixmap[hi])
                    {
                        /* handle memory allocation error */
                    }
                }
            }
        }
    }
    return image;
}

和第二个。看到几乎没有区别。

typedef struct {
unsigned int ** pixmap;
unsigned int height, width;
} PBMImage;

PBMImage * new_pbmimage( unsigned int w, unsigned int h)
{

    PBMImage *image;
    image = malloc(sizeof(*image));
    if(image)
    {
        image -> height = h;
        image -> width = w;
        image -> pixmap = malloc(h * sizeof(*image -> pixmap));
        if(image -> pixmap)
        {
            for(unsigned hi = 0; hi < h; hi++)
            {
                image -> pixmap[hi] = malloc(w * sizeof(**image -> pixmap));
                if(!image -> pixmap[hi])
                {
                    /* handle memory allocation error */
                }
            }
        }
    }
    return image;
}

我会使用灵活的数组成员和数组指针来实现它。只需要一个 mallocfree。删除了两级间接后效率更高。

typedef struct {
    size_t height, width, pixmax;
    unsigned int pixmap[][3];
} PPMImage;

PPMImage * new_ppmimage( const size_t w, const size_t h, unsigned int m )
{

    PPMImage *image;
    image =  malloc(sizeof(*image) +  h * w * sizeof(image -> pixmap[0]));
    if(image)
    {
        image -> height = h;
        image -> width = w;
    }
    return image;
}

简单用法:

unsigned int getPixelVal(PPMImage *image, const size_t x, const size_t y, const size_t index)
{
    unsigned int (*pixel)[image -> width][3] = image -> pixmap;

    return pixel[y][x][index];
}

这个

unsigned int ** pixmap[3];

表示 pixmap 是一个大小为 3 的数组,包含“指向无符号整数的指针”类型的值。不可能直接将任何东西分配给这样的数组类型。

您可能想做这样的事情:

image->pixmap[0] = tempArray;

这会将 tempArray 分配给 pixmap 的第一个元素。注意这些类型是如何匹配的。

重复分配过程,为 pixmap[1]pixmap[2] 分配内存。