首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用结构的2D内存分配

使用结构的2D内存分配
EN

Stack Overflow用户
提问于 2021-07-27 06:07:41
回答 2查看 67关注 0票数 0

//我必须创建一个新的imgr_t,其中包含初始大小行、cols和它们的保留对应项(行和reserved_rows将是相同的,而cols和reserved_cols是相同的)。如果分配成功(即内存分配成功),则返回指向新分配的imgr_t的指针,如果不成功,则返回空指针。

代码语言:javascript
复制
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

/* Structure type that encapsulates image: 2D array.
 * the rows represent the indices of the main array,
 * the cols represent the indices of the arrays pointed to by the pointers 
 * in the elements of the main array.
*/

typedef struct { 
    uint8_t** pixels;
    unsigned int rows;
    unsigned int cols;
    unsigned int reserved_rows;
    unsigned int reserved_cols;
} imgr_t;

/***** I am thinking something is wrong here but I cannot figure out what *****/

imgr_t* imgr_create(unsigned int rows, unsigned int cols){

    imgr_t* arr;
    arr->rows = rows;
    arr->cols = cols;
    arr->reserved_rows = rows;
    arr->reserved_cols = cols;
    arr = malloc(rows*sizeof(imgr_t));
    for(int i = 0; i<arr->rows; i++){
        arr->pixels[i] = malloc(cols*sizeof(imgr_t));
    }return arr;
}

void imgr_destroy(imgr_t* im){
    if(im != NULL){
        free(im->pixels);
        free(im);
    }
}

// helper function that prints the content of the img
void print_img(imgr_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(){

  imgr_t* test_im;
  printf("Creating test_im by calling 'img_create(10, 10)'\n");
  test_im = imgr_create(10, 10);
  printf("test_im created successfully.\n\n");

    
  return 0;
}

/*
    Output what I am getting : Creating test_im by calling 'img_create(10, 10)'
             signal: segmentation fault (core dumped)
*/
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-27 08:27:54

规则是需要调用free的次数与ons调用malloc的次数相同。uint8_t** pixels;不是指向2D数组的指针,而是指向数组指针的指针,因此您需要两次传递分配(在free时间相同)。

在这里你需要:

  • 1为imgr_t结构分配
  • 1为指向行的指针数组分配
  • 1每行(*)

代码可以是:

代码语言:javascript
复制
imgr_t* imgr_create(unsigned int rows, unsigned int cols) {

    imgr_t* arr = malloc(sizeof *arr);
    if (NULL == arr) return arr;       // do not forget to test allocation!
    arr->rows = rows;
    arr->cols = cols;
    arr->reserved_rows = rows;
    arr->reserved_cols = cols;
    arr->pixels = malloc(rows * sizeof(*arr->pixels));  // allocate rows
    if (NULL == arr->pixels) {         // another allocation to test
        free(arr);
        return NULL;
    }
    for (unsigned int i = 0; i < arr->rows; i++) {
        arr->pixels[i] = malloc(cols * sizeof(uint8_t));
        if (NULL == arr->pixels[i]) { // free what was allocated so far
            for (int j = 0; j < i; j++) {
                free(arr->pixels[i]);
            }
            free(arr->pixels)
            free(arr);
            return NULL;

        }
    }return arr;
}

void imgr_destroy(imgr_t* im) {
    if (im != NULL) {
        // one free per malloc...
        for (unsigned int i = 0; i < im->rows; i++) {
            free(im->pixels[i]);
        }
        free(im->pixels);
        free(im);
    }
}

小心:我不明白什么是保留,所以我忽略了它.

(*)实际上,可以在一次传递中分配整个数组,然后让指向行的指针指向全局数组中每一行的开始,但这是一种高级方法.

票数 0
EN

Stack Overflow用户

发布于 2021-07-27 06:10:01

代码语言:javascript
复制
imgr_t* imgr_create(unsigned int rows, unsigned int cols){

    imgr_t* arr;
    arr->rows = rows;
    arr->cols = cols;
    arr->reserved_rows = rows;
    arr->reserved_cols = cols;
    arr = malloc(rows*sizeof(imgr_t));
    for(int i = 0; i<arr->rows; i++){
        arr->pixels[i] = malloc(cols*sizeof(imgr_t));
    }return arr;
}

在这里,您在初始化arr之前使用它。您的初始化似乎是错误的,因为您只需要分配一个imgr_t元素。像素初始化在我看来也是错误的:类型不正确,您需要使用uint8_t类型。您还需要分配一些空间来存储像素:

代码语言:javascript
复制
imgr_t* imgr_create(unsigned int rows, unsigned int cols){

  imgr_t* arr = malloc(sizeof(imgr_t));
  arr->rows = rows;
  arr->cols = cols;
  arr->reserved_rows = rows;
  arr->reserved_cols = cols;
  arr->pixels = malloc(rows * sizeof(uint8_t*));

  for(int i = 0; i<arr->rows; i++){
    arr->pixels[i] = malloc(cols*sizeof(uint8_t));
  }

  return arr;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68539743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档