Okay, I think I'm having trouble understanding realloc. It seems to me that I've done things right, though I'm sure I'm missing a very obvious mistake. Here's the relevant part of my load function:

BYTE* buffer = malloc(sizeof(BYTE));

int count = 0;

for (BYTE c = fgetc(file); c != EOF; c = fgetc(file))
    buffer[count] = c;
    buffer = realloc(buffer, (sizeof(buffer) + sizeof(BYTE)));
    if (buffer == NULL)
        return false;

I originally tried using a second pointer, rather than count, but, of course, it would lose its place whenever buffer was realloc'ed. That change kept me from getting a bus error, but now I get this response:

GET / HTTP/1.1
HTTP/1.1 200 OK
GET /favicon.ico HTTP/1.1
*** Error in `./server': realloc(): invalid next size: 0x0000000001949990 ***

It's usually the subtle things that get us. Look at the following:

buffer = realloc(buffer, (sizeof(buffer) + sizeof(BYTE)));

buffer is a pointer. What is the sizeof( a pointer )? And what is the size of the total memory that buffer points at?

You need to find or keep track of the number of bytes already allocated, or have you already? ;-)

IF this answers your question, please click on the check mark to accept. Let's keep up with forum maintenance. ;-)

  • Ah, thank you! I see now. :)
    – Fobok
    Feb 17 '16 at 3:54
  • Ok, I fixed that. Using count + 1 instead of sizeof a pointer. ;) now, everything seems to work perfectly... Except cat.jpg isn't loading correctly. I'll ask about that in a separate question, though. :)
    – Fobok
    Feb 17 '16 at 5:15

You must log in to answer this question.

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