C 二维数组 Class

C 2d array Class

我必须创建一个具有初始大小行和列的新 img_t。如果成功(内存分配成功),returns指向新分配的指针img_t

我无法初始化行和列。

typedef struct { 
    uint8_t** pixels;
    unsigned int rows;
    unsigned int cols;
} img_t;

/// A type for returning status codes 

typedef enum {
    IMG_OK,
    IMG_BADINPUT,
    IMG_BADARRAY,
    IMG_BADCOL,
    IMG_BADROW,
    IMG_NOTFOUND
} img_result_t;

//something is wrong in this constructor*******
img_t* img_create(unsigned int rows, unsigned int cols){
        img_t **arr = malloc(rows * sizeof(img_t*));
        for(int i = 0; i < rows; i++){
           arr[i] = malloc(cols * sizeof(img_t));
           arr[i]->rows = rows;
      
           return arr[i]; 
        }
        return 0; 
    }

// helper function that prints the content of the img
void print_img(img_t* im) {
    if (im == NULL) {
        printf("Invalid img (null).\n");
        return;
    }

    printf("Printing img of row length %d and col length %d:\n", im->rows, im->cols);
    for (unsigned int i=0; i<im->rows; i++) {
        for (unsigned int j=0; j<im->cols; j++) {
            printf("%d ", im->pixels[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}
int main() {
    // test variables to hold values returned by the functions
    img_t* test_im = NULL;
    img_result_t test_result = IMG_OK;

    // test task 01 & 02
    printf("Creating test_im by calling 'img_create(10, 10)'\n");
    test_im = img_create(10, 10);
    if (test_im == NULL) {
        printf("test_im == NULL\n");
        return 1; //exit with a non-zero value
    }
    printf("test_im\n");
    printf("Printing test_im\n");
    print_img(test_im);
}

程序输出:

test_im
Printing test_im
Printing img of row length 10 and col length 0:

你的类型和尺寸到处都是。您正在构造一个 img_t * 大小的对象数组,长度为行,然后 return 在循环的第一次迭代中立即执行(在为您的结构分配过多内存之后)。

您没有设置 colspixels,并且正在泄漏 arr 持有的内存。

img_create 应该 return 一个指向 img_t 结构的指针。此结构包含 uint8_t 的二维数组和维度。

img_t *img_create(unsigned int rows, unsigned int cols) {
    img_t *img = malloc(sizeof *img);

    img->rows = rows;
    img->cols = cols;
    img->pixels = malloc(rows * sizeof *img->pixels);

    for (unsigned int i = 0; i < rows; i++)
        img->pixels[i] = calloc(cols, sizeof *img->pixels[i]);

    return img;
}

此示例使用 calloc 将每一行初始化为包含零。 *alloc 函数可能 return NULL - 你需要考虑在那种情况下该怎么做。

free这个结构,逆向操作。

另请注意,正确的 printf specifier for unsigned int is "%u", and for uint8_t it is defined in the macro PRIu8.

一个工作示例:

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    uint8_t **pixels;
    unsigned int rows;
    unsigned int cols;
} img_t;

img_t *img_create(unsigned int rows, unsigned int cols) {
    img_t *img = malloc(sizeof *img);

    img->rows = rows;
    img->cols = cols;
    img->pixels = malloc(rows * sizeof *img->pixels);

    for (unsigned int i = 0; i < rows; i++)
        img->pixels[i] = calloc(cols, sizeof *img->pixels[i]);

    return img;
}

void print_img(img_t *img) {
    if (img == NULL) {
        fprintf(stderr, "Invalid img (null).\n");
        return;
    }

    printf("Printing img of row length %u and col length %u:\n",
            img->rows, img->cols);

    for (unsigned int i = 0; i < img->rows; i++) {
        for (unsigned int j = 0; j < img->cols; j++)
            printf("%" PRIu8 " ", img->pixels[i][j]);

        printf("\n");
    }
}

void img_destroy(img_t *img) {
    for (unsigned int i = 0; i < img->rows; i++)
        free(img->pixels[i]);
    free(img->pixels);
    free(img);
}

int main(void) {
    img_t *i = img_create(10, 10);

    print_img(i);

    img_destroy(i);
}