Hot answers tagged

2 votes
Accepted

What will realloc() do if the contiguous memory location is already occupied?

The answer of our friend @NullityNull is correct, as long as realloc succeeds and works well. Your question is interesting. Consider the following program fragment char *string; string = (char *)...
user avatar
  • 5,172
2 votes

What will realloc() do if the contiguous memory location is already occupied?

If realloc can't resize the memory block you pass in, it makes a new one, copies the data, and deallocates the old one. If I were you I'd read up a bit on specification for Realloc and malloc.
user avatar
2 votes
Accepted

In pset6 server.c, does using realloc() reset the file pointer for fread()?

per fread's man page: size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by ...
user avatar
  • 17.4k
2 votes

How is my (custom) program leaking memory? I am preparing myself for pset5

You don't free the realloced memory. Your code buffer_temp_word = NULL; free(buffer_temp_word); means free is called for NULL. You should not change the value of ...
user avatar
  • 20.7k
1 vote
Accepted

realloc and length in load(), server.c

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 ...
user avatar
1 vote
Accepted

pset6 parse function: Invalid read of size 1

This is a tough one. It has to do with reusing p and realloc. I cannot find a specific technical explanation, but regardless, there are things that are problematic with this approach. First, you ...
user avatar
1 vote
Accepted

pset6 - load(), realloc() and 'invalid next size' error

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 ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible