cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: why "mem->memory[mem->size] = 0;" in example getinmemory.c ?

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Sat, 18 Jun 2011 09:42:18 +0200 (CEST)

On Sat, 18 Jun 2011, Rieker Flaik wrote:

> 1. valgrind says that there would be a leak in:
>
> WriteMemoryCallback():
> mem->memory[mem->size] = 0;
>
> what does this line do?

A leak? It is just an assignment so it can't cause a leak. It zero terminates
the data simply because most people get text-like data with the example and
uses printf() or similar to output it.

> 2. in main():
> chunk.memory = malloc(1);
>
> Why do I need this malloc(1)? Why can't realloc do the rest?

realloc _does_ the rest.

> If I omit this line, the program crashes.

... which is why malloc is used there. Not all reallocs like reallocing NULL.

-- 
  / daniel.haxx.se
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
Received on 2011-06-18