I implemented the load function, I compiled it without errors, but if I run it with gdb I get segmentation fault, sometimes after the first fread, sometimes after the second. That's my code:
bool load(FILE* file, BYTE** content, size_t* length)
{
*content = malloc(sizeof(BYTE));
*content = NULL;
*length = 0;
//stores the address of the first byte in *content
fread(*content, sizeof(BYTE), 1, file);
if(*content == NULL)
{
printf("nothing to load");
free(*content);
return false;
}
else
*length = 1;
BYTE* new_byte = malloc(sizeof(BYTE));
new_byte = NULL;
//reads all available bytes from file and stores those bytes contiguously in dynamically allocated memory on the heap
while(fread(new_byte, sizeof(BYTE), 1, file))
{
int i = *length;
*length = *length + 1;
*content = realloc(*content, sizeof(BYTE) * (*length));
*content[i] = *new_byte;
}
return true;
}
Can someone help me? Thanks