c – realloc(): invalid next size when reallocating to make space for strcat on char *

c – realloc(): invalid next size when reallocating to make space for strcat on char *

This error occurs because some other part of your code has corrupted the heap. We cant tell you what that error is without seeing the rest of the code.

The fact that FINE 7 is not printed tells you that realloc is failing. And that failure must be because buffer is invalid due to a heap corruption earlier in the execution.


Orthogonal to your actual problem, sizeof(char) is 1 by definition so it makes sense to remove it from the code.

As David Heffernan points out, your root problem must be a wild pointer elsewhere in your code smashing the heap.

There are several other things worth thinking about in this code snippit, though:

  1. No need for sizeof (char) in the new size expression, as sizeof (char) is, by definition, 1.

  2. Never assign the return from realloc directly back to the only pointer to the buffer youre reallocating. If realloc returns NULL on an error, youll lose your pointer to the old buffer, and gain your very own memory leak. You always want to do the appropriate equivalent of:

    footype *p = realloc(oldbuff, newsize);
    if (!p) {
        handle_error();
    } else {
        oldbuff = p;
    }
    
  3. In C, void * will automatically be converted to the correct type on assignment, there is no need to cast. Further, by casting, in some cases you wont get helpful error messages when you forget to include the declaration of the function in question.

  4. String literals include an implied nul terminator. You wanted to say:

    strcat(buffer, : );

On the up side, strcat will stop at the first nul character, so no harm in this case.

c – realloc(): invalid next size when reallocating to make space for strcat on char *

(char *)realloc(buffer, strlen(buffer)* sizeof(char) + (strlen(: )+1)* sizeof(char));

Should be

(char *)realloc(buffer, (strlen(buffer) + strlen(: ) + 1) * sizeof(char));

should it not? Youre math for the length of the string is wrong.

Leave a Reply

Your email address will not be published.