0

Hello i am trying to apply a filter over a greyscale pgm image:

int contor = 0;
char *magicNumber;
char *metaData;
int imageWidth = 0, imageHeight = 0, valueRange = 0;
int *imageData;
while (EOF != fscanf(fp2, "%[^\n]\n", line))
{
    if(contor == 0)
    {
        magicNumber = (char*)malloc((1 + strlen(line)) * sizeof(char));
        strcpy(magicNumber, line);
        magicNumber[strlen(magicNumber)] = '\0';
    }
    else if (contor == 1)
    {
        metaData = (char*)malloc((1 + strlen(line)) * sizeof(char));
        strcpy(metaData, line);
        metaData[strlen(metaData)] = '\0';
    }
    else if (contor == 2)
    {
        token = strtok(line, " ");
        imageWidth = atoi(token);
        token = strtok(NULL, " ");
        imageHeight = atoi(token);
        imageData = (int*)malloc(imageWidth * imageHeight * sizeof(int));
    }
    else if (contor == 3)
    {
        valueRange = atoi(line);
    }
    else
    {
        printf("Should put value %d in imageData at index %d\n", atoi(line), contor);
        //imageData[contor-4] = atoi(line);
    }
    contor++;
}

My code breaks at the imageData memory allocation. width and height are correctly read and converted to int;

Can anyone tell me what i am doing wrong ?

mch
  • 9,424
  • 2
  • 28
  • 42
user1840302
  • 189
  • 3
  • 13
  • 2
    In C you shouldn't cast a malloc - see here http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Philipp Jan 12 '17 at 09:02
  • removing the cast will yeld this warning: assignment makes integer from pointer without a cast [enabled by default] imageData[j] = (int*)malloc(imageHeight * sizeof(int)); And still doesn't fix the problem – user1840302 Jan 12 '17 at 09:07
  • I hope you didn't forgot to include . http://stackoverflow.com/questions/21327899/when-we-used-malloc-without-declaring-stdlib-h-header-file-compiler-returns-an-int why? – achoora Jan 12 '17 at 09:09
  • Additionally to what achoora said - that is exactly the reason mentioned in the answer I linked to why you shouldn't cast malloc because it can hide an error like this. – Philipp Jan 12 '17 at 09:11
  • stdlib was included in both cases. Additional info: overwriting the image's witdth and height works. Read from the pgm file the values are w=3840 and h = 2160. Overwritting them to 10 seemed to work. – user1840302 Jan 12 '17 at 09:12
  • And manually asisgning these values for check purposes crashes the program as well. May it be that i cannot allocate memory for 3840 * 2160 ints ? – user1840302 Jan 12 '17 at 09:18
  • 1
    If there would be not enough memory available, you would get a NULL pointer (you should check if malloc returns a NULL-Pointer), but the warningn with the integer from pointer should be investigated – Kami Kaze Jan 12 '17 at 09:20
  • 1
    I'm failing to see who this question relates to MPI. – Hristo Iliev Jan 12 '17 at 09:21
  • Just under the allocation i put the following code if(imageData == NULL) printf("NOT ENOUGH MEMORY"); Nothing showed up. No print after that line works Indeed there is no MPI code in what i provided. This bit of code is run by the master thread. I thought i should mention MPI in case it has some restrctions i am not aware of – user1840302 Jan 12 '17 at 09:29
  • 1
    So you are saying the allocation actually works and the program crashes somewhere else? I think a MWE would help here. – Philipp Jan 12 '17 at 09:30
  • No print works after exactly that line. it crashes there. Check for NULL or not the code doesn't reach the line following the allocation – user1840302 Jan 12 '17 at 09:35
  • Possible duplicate of [Malloc segmentation fault](http://stackoverflow.com/questions/22051294/malloc-segmentation-fault) – n0p Jan 12 '17 at 09:50

0 Answers0