I am stucked at the load() function. This line :

newBufferSize = bufferSize * (*length);

gives me the following error:

realloc(): invalid next size: 0x0000000000607be

Then the process was aborted.

I tried to debug it, and saw that things actually went well for the beginning loops, but failed as the length grew.

I really can't figure out the problem, someone please help.

My load() function:

bool load(FILE* file, BYTE** content, size_t* length)
    if (file == NULL) {
        return false;

    *content = NULL;
    *length = 0;

    int newBufferSize;
    int bufferSize = sizeof(BYTE);
    int* buffer = malloc(bufferSize);

    if (buffer == NULL) {
        printf("Could not create buffer for reading file content.");
        return false;

    while(fread(buffer, bufferSize, 1, file) == 1 && !feof(file)){
        *length += 1;

        newBufferSize = bufferSize * (*length);

        *content = realloc(*content, newBufferSize);

        if (*content == NULL) {
            return false;

        memcpy(*content + newBufferSize, buffer, newBufferSize);

    return true;

Look at this line:

memcpy(*content + newBufferSize, buffer, newBufferSize);

Then go read memcpy documentation: https://www.tutorialspoint.com/c_standard_library/c_function_memcpy.htm

Do you see now that you used the wrong variable as the third argument?

Pay attention that buffer only holds one byte, but newBufferSize is increasing each iteration... you're copying beyond the buffer limit (which is just one byte) and you don't know what's beyond that limit!

That will cause a seg fault sooner or later and that's what's breaking your code.

Other than that, you have another problem, but I'll let you fix this one first and let you think about the other problem when it arises.

  • 1
    Thanks a lot! I figured out the other problem as well. How to calculate the memory sizes right is a real headache. Phew. – Levblanc Oct 20 '16 at 4:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .