curl-library
Re: why "mem->memory[mem->size] = 0;" in example getinmemory.c ?
Date: Sat, 18 Jun 2011 10:45:31 +0000
These are commonly asked questions. I mean no disrespect to the poster. Maybe there ought to be more documentation in the source code?
> mem->memory[mem->size] = 0;
> what does this line do?
It puts a zero at the end of the data that has been copied into the buffer. That zero will be overwritten if and when there is a subsequent data-write, or it will function to mark the end of the buffer if it is the final data-write.
> chunk.memory = malloc(1);
> Why do I need this malloc(1)? Why can't realloc do the rest? If I omit
> this line, the program crashes.
You need malloc(1) to create a buffer that can subsequently be passed to realloc(). malloc(0) might make more sense, but some implementations won't allocate a buffer of 0 size and pass a useful pointer to it in response to malloc(0).
What I did in my code was to have a pointer to buffer, and a size_t that identified the buffer size. They are both initialized to zero. When the callback function executes, if there is a pointer, it calls realloc(). If there is no valid pointer, it calls malloc(). I've just shifted the malloc() into the callback. No biggie.
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2011-06-18