Originally Posted by
Salem
You're not allocating memory.
chunk.memory = malloc( 1024 );
For example.
Then read up to 1024 bytes into the memory.
The data is created with these functions:
Code:
void *myrealloc(void *ptr, size_t size)
{
// There might be a realloc() out there that doesn't like reallocing NULL pointers, so we take care of it here
if(ptr)
return realloc(ptr, size);
else
return malloc(size);
}
size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
register int realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;
mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}
It's for use with the cURL C Libraries if you know them at all.
Originally Posted by
Salem
Is your data always '\0' terminated, like a proper C-string should be?
It needs to be if things like strcpy, strlen, strtok are to work.
Yes, and as I said, I did try appending a new \0 to the string and then using the functions.
Originally Posted by
Salem
> chunk.memory=&chunk.memory[1];
This is very bad - never modify a pointer to memory you allocated. You'll find it extremely hard to free that memory in future.
The most correct thing to do here is use memmove() to shuffle the entire string back one place in memory.
Thanks for the tip, this is my first project in C (personal as opposed to homework )