您好我正在尝试在灰度pgm图像上应用滤镜:
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++;
}
我的代码在imageData内存分配中断。正确读取宽度和高度并将其转换为int;
谁能告诉我我做错了什么?