I need help with load() in server.c. This is a follow-up to the other question, except now I'll post the updated code and make it a bit more specific.

When I tested the server after implemented lookup and parse (with abs_path and query), and had had done some stuff on load as well, I got this output:

Using /home/ubuntu/workspace/pset6/public for server's root

Listening on port 8080

GET / HTTP/1.1

HTTP/1.1 200 OK

GET /cat.html HTTP/1.1

server: malloc.c:2842: mremap_chunk: Assertion `((size + offset) & (_rtld_global_ro._dl_pagesize - 1)) == 0' failed.


I tried to make some changes to my realloc call in load after that, and right now the function looks like this:

bool load(FILE* file, BYTE** content, size_t* length)
    int c;
    *length = 0;
    while ((c = fgetc(file)) != EOF)
        *content = realloc(*content, *length + 1);
        if (*content == NULL)
    return true;

This also doesn't compile because the value of length after length++ is unused (the main culprit being the -Werror compilation flag sent to Clang by make).

Any help would be appreciated, short of just giving me the answer of course.

By the way, how many bytes should I read with realloc? Is *length + 1 correct?

1 Answer 1


Your code is almost functional, but you're forgetting one thing and you have two errors.

What you're forgetting

You need to transfer to *content each byte you're putting inside c. Otherwise you'll simply overwrite c hundreds of times for nothing.


The first and most obvious error is that you shouldn't free() the content, otherwise you'll lose it. The staff wrote code in main() that will do the job of freeing content when the server no longer needs it.

The second and more subtle error is that warning the compiler is giving you about the length.

That happens because *length++ is not doing what you think. It's doing this:

  1. The dereference (*) happens first, giving you the contents of the memory location indicated by length. Let's suppose the number there is 100.
  2. Then the expression is evaluated, the result of 100 is still 100.
  3. The result is thrown away (you aren't doing anything with it).
  4. length is then incremented AFTER the evaluation. It's changing where the pointer is pointing to, not what it's pointing at.

It is effectively the same as doing this:

length = length + 1;

So the fix would be either:
1. use (*length)++ instead of *length++ to override the order of preference between the * operator and the ++.
2. go the simple way and use *length += 1; or *length = *length + 1;

If this answers your question, please click the check mark to accept it.

  • Thanks for the reply. So to put c into *content, would *content[c] = c be correct? Or is that completely wrong? Dec 1, 2016 at 8:28
  • I'm getting a Segmentation Fault error and an invalid pointer error with realloc. > *** Error in `/home/ubuntu/workspace/pset6/server': realloc(): invalid pointer: 0x00007fffffffde90 *** >Program received signal SIGABRT, Aborted. > 0x00007ffff7745c37 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56 I'll post the code on pastebin and give the link in this thread. Dec 1, 2016 at 8:39
  • Link: pastebin.com/dF90fneK Dec 1, 2016 at 8:49
  • Update: pastebin.com/dF90fneK Dec 1, 2016 at 11:34
  • I'm getting these errors when I try to compile it now: server.c:627:13: error: called object type 'size_t' (aka 'unsigned long') is not a function or function pointer (*length)++; ^ server.c:627:13: error: invalid operands to binary expression ('size_t' (aka 'unsigned long') and 'size_t *' (aka 'unsigned long *')) *length += 1; ^~~~~~~ Dec 1, 2016 at 11:46

You must log in to answer this question.

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