Hot answers tagged

2

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 *)malloc(100); string = NULL; One of the most common failure when memory is managed explicitly is what is known as "memory leak". This situation occurs when a ...


2

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.


2

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 stream, storing them at the location given by ptr. per realloc's man page: The realloc() function returns a pointer to the newly allocated memory, ...


2

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 buffer_temp_word there, you could increment a copy instead, or use an index variable. And 8 bytes is correct, in both cases you lose the memory when you are at a char_count ...


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. Errors: The first and most obvious error is that you shouldn't free() the content, otherwise you'll lose it. ...


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 cannot depend on realloc to keep the same address. It'll start out with p = method;, ie p pointing to the same address as method. But if realloc "moves" p, then ...


1

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'...


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