-2

Now I'm trying to implement the binary data read and manipulate by using ANSI C. I've define the struct type as the follow.

typedef struct {
    int     width;
    int     height;
    int     max;
    unsigned char **dummy;
    unsigned char **pixels;
} PPMImage;

then in the main function, I can read the binary data.

int  main(int argc, char **argv)
{
    FILE *fpIn, *fpOut;
    PPMImage    img;

    if (fnReadPPM(argv[1], &img) != TRUE){
        return -1;
    } until here success

but when I run this code, I've got a access violation. I don't know the reason, How can I access the structure type in call by reference?

    if (fnProc_grayPPM(&img) != TRUE){
        return -1;
    }

...
}


int fnReadPPM(char* fileNm, PPMImage* img)
{
    FILE* fp;

    if (fileNm == NULL){
        return FALSE;
    }

    fp = fopen(fileNm, "rb");   // binary mode
    if (fp == NULL){
        return FALSE;
    }

    img->pixels = (unsigned char**)calloc(img->height, sizeof(unsigned char*));
    img->luminance = (unsigned char**)calloc(img->height, sizeof(unsigned char*));

    for (int i = 0; i<img->height; i++){
        img->pixels[i] = (unsigned char*)calloc(img->width * 3, sizeof(unsigned char));
        img->luminance[i] = (unsigned char*)calloc(img->width , sizeof(unsigned char));
    }

    for (int i = 0; i<img->height; i++){
        for (int j = 0; j<img->width * 3; j++){
            fread(&img->pixels[i][j], sizeof(unsigned char), 1, fp);
        }
    }

    fclose(fp);

    return TRUE;
}




int fnProc_grayPPM(PPMImage* img)
{
    for (int i = 0; i < img->height; i++){
        for (int j = 0; j < img->width ; j += 3){

            img->luminance[i][j] = (img->pixels[i][j]); //<--- have a violation.

        }
    }

    return TRUE;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
chiper
  • 31
  • 3
  • 1
    [Please see this discussion on why not to cast the return value of malloc() and family in C..](https://stackoverflow.com/q/605845/2173917) – Sourav Ghosh Jun 12 '17 at 05:33
  • 1
    The code you have posted does not compile. It also does not look representative oof the code you tested. – EOF Jun 12 '17 at 05:35
  • What is `luminance` ? There are no such member in `PPMImage`. No way this code compiles. Please post the correct code. – Support Ukraine Jun 12 '17 at 06:11

1 Answers1

1

Your call to calloc() is erroneous. You have used

  (unsigned char**)calloc(img->height, sizeof(unsigned char*));

but img->height contains indeterminate value at that point. The result cannot be deterministic.

To elaborate, img was a local automatic variable which was not initialized explicitly. Thus, the individual member variables of the structure variable contains indeterminate value. So, before you read the variables, you need to write some value into it.

That said, please see this discussion on why not to cast the return value of malloc() and family in C..

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • when I check the img->height with break point, I can check the determinate value is valid at that point. the the result can be deterministic. – chiper Jun 12 '17 at 05:47