【发布时间】:2021-10-04 18:08:03
【问题描述】:
我必须确保 row 和 col 有效,我必须将 im->pixels[row][col] 的值设置为 val 并返回 IMG_OK。
否则,数组将不被修改并返回。我知道问题出在img_result_t img_set(),但我无法弄清楚。
我无法将 val 设置为我的数组。在运行main() 时,我得到的输出是,
Creating test_im by calling 'img_create(10, 10)'
test_im created successfully.
Testing img_set.
Cannot set value at index 0
代码:
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
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;
img_t *img_create(unsigned int rows, unsigned int cols){
img_t* arr = malloc(sizeof(img_t));
if(arr == NULL) return arr;
arr->rows = rows;
arr->cols = cols;
arr->pixels = malloc(rows * sizeof(*arr->pixels));
if(arr->pixels == NULL){
free(arr);
return NULL;
}
for(unsigned int i = 0; i<arr->rows; i++){
arr->pixels[i] = malloc(cols*sizeof(img_t));
if(arr->pixels[i] == NULL){
for(int j= 0; j < i; j++){
free(arr->pixels[i]);
}
free(arr->pixels);
free(arr);
return NULL;
}
}return arr;
}
void img_destroy(img_t* im){
if(im != NULL){
for(unsigned int i = 0; i < im->rows; i++){
free(im->pixels[i]);
}
free(im->pixels);
free(im);
}
}
img_result_t img_set(img_t* im, unsigned int row, unsigned int col, int val){
if(im == NULL) return IMG_BADARRAY;
im->rows = row;
im->cols = col;
unsigned int empty = 0;
if(row <= empty){
return IMG_BADROW;
}
if(col <= empty){
return IMG_BADCOL;
}
im->pixels[row][col] = val;
return val;
}
// 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_t* null_im = NULL;
img_t* test2_im = NULL;
img_result_t test_result = IMG_OK;
int val;
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 created successfully.\n\n");
printf("Testing img_set.\n");
for (unsigned int i=0; i<test_im->rows; i++) {
for (unsigned int j=0; j<test_im->cols; j++) {
if (img_set(test_im, i, j, (rand()%100)) != IMG_OK) {
printf("Cannot set value at index %d\n", i);
return 1; //exit with a non-zero value
}
}
}
}
【问题讨论】:
-
进行基本调试。
img_set中的哪个条件失败?使用调试器逐步执行代码。if(row <= empty)和if(col <= empty)问问你自己row和col的值是什么,你用什么来调用函数,为什么会触发这些错误条件?您可以通过任何基本的调试量轻松找到它。 How to debug small programs -
您在
img_set()的末尾有return val;,但您应该返回IMG_OK。在您知道一切正常之前,您不应该更改任何内容,但是您从该位置设置了im->rows和im->cols。这可能意味着当您设置row = 0和col = 0像素时,您的图像会缩小。 -
当您检查
img_set(test_im, i, j, (rand()%100)) != IMG_OK时,该函数也会执行return val; -
我认为最后的评论是一个答案,可能是答案。 @kaylum 如果您想回答并希望我删除我的,请告诉我。
标签: c memory-management malloc heap-memory dynamic-memory-allocation