0

I currently use a cUrl Ftp Get function that downloads a file from ftp server and saves it on the local computer, and it works perfectly. What I need to do is make the next step and get the file from server without saving it directly but putting it in memory. For this purpose, I know there is another cUrl function, Ftp Get in Memory but can't figure it out why it doesn't work. Is there any implementation of this function in C++?

Problems I get: "a value of type void* cannot be used to initialize an entity of type char*" Tried to do some casting but nothing. This happens on malloc(1) and realloc().

John
  • 1
  • 1
  • What have you tried and what errors are you getting? – Jaffa Mar 06 '20 at 16:40
  • Mostly realloc() function, defined in the callback function. In theory it should allocate a chunk of memory where data needs to be stored. Firstly it was seen as error by Visual Studio, then I tried to make it work, using other functions but it had always failed. If there is an example I would appreciate that. – John Mar 06 '20 at 16:47
  • What is the error you're getting? `realloc` is available in `` – Jaffa Mar 06 '20 at 16:55
  • "a value of type void* cannot be used to initialize an entity of type char*" Tried to do some casting but nothing. Same happens on malloc(1). I suppose it's because the function is declared in C – John Mar 06 '20 at 17:04
  • It is usually better to directly provide the error in your post, by editing it, so that anyone can see what your problem was. I edited my answer to add a link to a question answering your problem with realloc. – Jaffa Mar 06 '20 at 17:10
  • Maybe you should show us the code where the error is... – user253751 Mar 06 '20 at 18:18

1 Answers1

1

This example shows how to do exactly that:

static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
  size_t realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)userp;

  char *ptr = static_cast<char*>(realloc(mem->memory, mem->size + realsize + 1));
  if(ptr == NULL) {
    /* out of memory! */ 
    printf("not enough memory (realloc returned NULL)\n");
    return 0;
  }

  mem->memory = ptr;
  memcpy(&(mem->memory[mem->size]), contents, realsize);
  mem->size += realsize;
  mem->memory[mem->size] = 0;

  return realsize;
}


curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

Check this post and this post to know how to correctly cast malloc/realloc return value in C++

Jaffa
  • 12,442
  • 4
  • 49
  • 101
  • This is exactly the libcurl function, written in C which I can't make it work, if there is an example of this in C++ which actually works I'd really appreciate it. – John Mar 08 '20 at 16:09
  • @John have you visited the link I posted? They talk specifically about how malloc/realloc return value... – Jaffa Mar 08 '20 at 16:20
  • Yes I visited it but couldn't get it done. That whole function you posted is what I already had weeks ago and that's not my question. Thanks anyway – John Mar 08 '20 at 22:26
  • That's what you asked for, if it's not then I advise you to delete your question and ask a new one, where you post your specific problem and errors, I'll be happy to help – Jaffa Mar 09 '20 at 10:41