简体   繁体   English

C : 生成随机矩阵的函数返回最后一行空值

[英]C : function that generates random Matrix returns last line full of null values

I've written this little piece of code that returns a random matrix of pixels.我写了一小段代码,它返回一个随机的像素矩阵。 Each pixels has random values for its RGB colours.每个像素的 RGB 颜色都有随机值。 After generating the matrix, I print it.生成矩阵后,我打印它。

In this specific example, I try to generate and print a 10 x 10 matrix :在这个特定示例中,我尝试生成并打印一个 10 x 10 矩阵:

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
    
#define nbLines 10
#define nbColumns 10
    
struct Pixel {
    uint8_t R,G,B;
};
    
struct Image{
    struct Pixel* matrix[nbLines];
};
    
void returnRandomImage(struct Image* image){
    srand(time(NULL));
    int lower = 0, upper = 255;
    
    for(int i = 0; i < nbLines; i++){
        image->matrix[i] = (struct Pixel*)malloc(nbColumns*sizeof(struct Pixel));
        for(int j = 0; j < nbColumns ;j++){
            image->matrix[i]->R = (rand() % (upper - lower + 1)) + lower;
            image->matrix[i]->G = (rand() % (upper - lower + 1)) + lower;
            image->matrix[i]->B = (rand() % (upper - lower + 1)) + lower;
            (image->matrix[i])++;
        }
    }
}
    
void printMatrix(struct Image* image){
    for(int i = 0; i < nbLines; i++){
        for(int j = 0; j < nbColumns;j++){
            printf("(%d,%d,%d) ",image->matrix[i]->R,image->matrix[i]->G,image->matrix[i]->B);
            (image->matrix[i])++;
        }
        printf("\n");
    }
}
    
int main(int argc, char const *argv[]) {
    struct Image image;

    returnRandomImage(&image);
    printMatrix(&image);
    
    return 0;
}

The output is :输出是:

(0,0,74) (116,199,63) (89,54,221) (58,125,129) (44,213,248) (199,195,15) (48,1,228) (138,30,62) (118,0,44) (245,191,116) 
(0,0,199) (27,116,213) (200,93,48) (116,1,84) (106,64,74) (108,118,222) (87,169,87) (59,205,237) (162,240,113) (121,230,31) 
(0,0,182) (242,58,254) (41,246,199) (153,15,92) (213,200,89) (142,59,114) (201,154,110) (110,142,3) (111,231,254) (104,178,159) 
(0,0,22) (82,114,23) (233,219,199) (236,131,163) (205,137,207) (31,65,130) (31,122,127) (2,204,191) (37,93,195) (6,144,46) 
(0,0,241) (10,110,91) (51,14,222) (3,83,176) (159,225,57) (175,199,137) (144,129,209) (115,180,27) (117,104,16) (241,226,98) 
(0,0,114) (173,241,143) (163,94,88) (53,208,140) (130,184,121) (59,185,19) (206,159,1) (109,49,209) (208,72,38) (192,8,101) 
(0,0,46) (54,158,225) (122,40,218) (93,144,35) (32,25,148) (235,33,77) (58,27,2) (177,123,130) (206,74,110) (183,140,53) 
(0,0,38) (6,197,217) (65,123,229) (75,38,132) (46,76,55) (239,236,200) (91,87,117) (54,138,74) (15,201,80) (103,137,121) 
(0,0,168) (241,162,37) (49,129,106) (92,169,1) (208,185,212) (239,67,125) (18,151,7) (51,49,190) (116,245,202) (116,173,204) 
(0,0,0) (0,0,0) (0,0,0) (0,0,0) (0,0,0) (0,0,0) (0,0,0) (0,0,0) (0,0,0) (0,0,0) 

I dont understand why the last line is full of null values and why the first columns has the R and G colours null as well.我不明白为什么最后一行充满空值以及为什么第一列的 R 和 G 颜色也为空。

I discovered that the bug is fixed when the code iterates through the for loops in the function returnRandomImage 11 times instead of 10 for a 10 x 10 matrix as shown below :我发现当代码在函数returnRandomImage的 for 循环中迭代 11 次而不是 10 次(对于 10 x 10 矩阵)时,该错误得到了修复,如下所示:

void returnRandomImage(struct Image* image){
    srand(time(NULL));
    int lower = 0, upper = 255;
    
    for(int i = 0; i <= nbLines; i++){ //ITERATES 11 TIMES
        image->matrix[i] = (struct Pixel*)malloc(nbColumns*sizeof(struct Pixel));
        for(int j = 0; j <= nbColumns ;j++){ //ITERATES 11 TIMES
            image->matrix[i]->R = (rand() % (upper - lower + 1)) + lower;
            image->matrix[i]->G = (rand() % (upper - lower + 1)) + lower;
            image->matrix[i]->B = (rand() % (upper - lower + 1)) + lower;
            (image->matrix[i])++;
        }
    }
}

Keep in mind that the function printMatrix still iterates 10 times only.请记住,函数printMatrix仍然只迭代 10 次。 The ouput is :输出是:

(254,43,214) (237,8,219) (3,50,100) (73,200,237) (62,18,91) (31,248,25) (133,229,154) (250,32,102) (13,228,113) (17,246,129) 
(118,70,64) (153,190,96) (20,127,153) (106,164,63) (145,102,133) (196,5,184) (108,229,142) (64,181,85) (154,76,109) (148,104,38) 
(121,176,223) (121,120,88) (10,206,115) (243,44,220) (225,129,24) (135,110,131) (53,73,69) (137,222,254) (3,255,99) (114,169,194) 
(79,57,144) (167,148,107) (181,39,72) (217,208,81) (60,175,191) (23,107,14) (107,47,221) (233,109,230) (44,33,237) (188,72,178) 
(123,148,139) (132,108,71) (199,246,248) (250,237,180) (214,103,73) (6,160,116) (166,101,25) (59,125,53) (49,27,47) (72,158,82) 
(43,240,56) (36,98,101) (146,154,72) (18,90,124) (207,222,34) (10,180,17) (90,138,72) (8,214,232) (20,189,224) (201,182,63) 
(126,147,89) (20,53,68) (54,107,108) (88,85,122) (77,29,94) (118,189,53) (91,216,192) (89,68,45) (22,238,165) (197,113,81) 
(182,55,121) (5,70,52) (148,57,71) (134,139,10) (250,185,174) (182,43,51) (192,59,220) (98,125,212) (140,61,28) (3,115,215) 
(194,40,59) (19,187,177) (140,225,39) (227,187,0) (100,125,240) (6,173,41) (193,31,79) (146,218,192) (140,47,135) (26,230,164) 
(157,137,85) (247,99,135) (22,123,89) (44,156,166) (201,170,52) (211,109,149) (30,20,244) (249,186,190) (169,215,91) (197,105,69)

Above is the desired result.以上是想要的结果。

Any explanation for this behavior?这种行为有什么解释吗?

OP's loop re-assigns the allocated pointer with (image->matrix[i])++ : OP 的循环使用(image->matrix[i])++重新分配分配的指针:

    image->matrix[i] = (struct Pixel*)malloc(nbColumns*sizeof(struct Pixel));
    for(int j = 0; j <= nbColumns ;j++){ //ITERATES 11 TIMES
        image->matrix[i]->R = (rand() % (upper - lower + 1)) + lower;
        image->matrix[i]->G = (rand() % (upper - lower + 1)) + lower;
        image->matrix[i]->B = (rand() % (upper - lower + 1)) + lower;
        (image->matrix[i])++;
    }

This loses the original pointer value for later use.这将丢失原始指针值以供以后使用。

Code also iterates 1 too often (or allocates 1 pixel short)代码也太频繁地迭代 1(或分配 1 个像素短)


Instead, access the pixel array with j as image->matrix[i] best to leave "as is" until free() (which is missing).相反,使用j as image->matrix[i]访问像素数组最好保持“原样”,直到free() (缺少)。

Allocation made more C idiomatic too.分配也使 C 变得更加惯用。

    image->matrix[i] = malloc(sizeof *(image->matrix[i]) * nbColumns);
    if (image->matrix[i]) { 
      //               v   Iterate 10 times
      for(int j = 0; j < nbColumns ;j++){
          image->matrix[i][j].R = (rand() % (upper - lower + 1)) + lower;
          image->matrix[i][j].G = (rand() % (upper - lower + 1)) + lower; 
          image->matrix[i][j].B = (rand() % (upper - lower + 1)) + lower;
          //(image->matrix[i])++;
      }
    }

Similar problem in printing.打印中的类似问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM