我在这个函数中有一个段错误......它给我的错误消息是这个......
*** glibc detected *** /double free or corruption (fasttop): 0x0000000000603250 ***
*** glibc detected *** / memory corruption: 0x00007ffff7dd3710 ***
这是我的所有代码:
struct Image * loadImage(const char* filename)
{
//VARIABLES
struct ImageHeader * hdr2; // on heap, malloc, free
FILE * fptr = fopen(filename, "r");
if (fptr == NULL)
{
return NULL;
}
int retval;
hdr2 = malloc(sizeof(struct ImageHeader));
if (hdr2 == NULL)
{
free(hdr2);
fclose(fptr);
return NULL;
}
retval = fread(hdr2, sizeof(struct ImageHeader), 1, fptr);
if (retval != 1)
{
free(hdr2);
fclose(fptr);
return NULL;
// error
}
if (hdr2 -> magic_bits != ECE264_IMAGE_MAGIC_BITS)
{
free(hdr2);
fclose(fptr);
return NULL;
// error
}
if (hdr2->width == 0||hdr2->height ==0)
{
free(hdr2);
fclose(fptr);
return NULL;
// error
}
struct Image * img = NULL;
img = malloc(sizeof(struct Image));
if(img == NULL)
{
fclose(fptr); // free(img);
return NULL;
//do something
}
img -> width = hdr2 -> width;
img ->height=hdr2->height;
img -> comment = malloc(sizeof(char) * (hdr2->comment_len));
img -> data = malloc(sizeof(uint8_t) * hdr2->width * hdr2->height);
retval = fread(img->comment, sizeof(char), hdr2->comment_len, fptr);
if(img -> comment == NULL)
{
free(hdr2);
free(img->data);
free(img->comment);
free(img);
fclose(fptr);
return NULL;
//do something, don't forget to free whatever you have allocated
}
//lookg at the img-> comment (should free)
if (retval != hdr2->comment_len)
{
free(hdr2);
free(img->data);
free(img->comment);
free(img);
fclose(fptr);
return NULL;
// error
}
/*
if(img->comment[hdr2->comment_len-1]=='\0')/////////////////////////THIS IS THE PROBLEM AREA
{
free(hdr2);
free(img->data);
free(img->comment);
free(img);
fclose(fptr);
return NULL;
}*/
if(img->data==NULL)
{
free(hdr2);
free(img->comment);
free(img);
fclose(fptr);
return NULL;
}
retval = fread(img->data, sizeof(uint8_t),hdr2->width * hdr2->height, fptr);
if (retval != (hdr2->width * hdr2->height))
{
free(hdr2);
free(img->data);
free(img->comment);
free(img);
fclose(fptr);
return NULL;
}
uint8_t j = 0;
if(fread(&j, sizeof(uint8_t),2,fptr)==2)
{
free(hdr2);
free(img->data);
free(img->comment);
fclose(fptr);
}
retval = fread(img ->data, sizeof(uint8_t), hdr2->width * hdr2->height +1, fptr);
if(retval==hdr2->width*hdr2->height+1)
{
free(hdr2);
free(img);
//error
fclose(fptr);
return NULL;
}
fclose(fptr);
return (img);
}
void freeImage(struct Image * image)
{
if(image!=NULL)
{
free(image->data);
free(image->comment);
}
free(image);
}
void linearNormalization(struct Image * image)
{
int index1 = 0;
int index2 = 0;
//int totaldata = image->height * image->width;
int max = 0;
int min = 255;
// if(image -> data[i] > max)
// set max equal to this image
for(index1=0; index1<(image->height * image->width); index1++)
{
if(image->data[index1] > max)
{
max= image->data[index1];
}
if(image->data[index1]<min)
{
min = image->data[index1];
}
}
for(index2 = 0; index2<(image->height * image->width);index2++)
{
image->data[index2] = (image->data[index2]- min) * 255.0 / (max - min);
}
}
我标记了给我带来问题的区域......当我将它留在我的函数中时,它不会输出正确的数据,但不会出现段错误。有人能告诉我这个错误代码对那条线意味着什么吗?