Dynamic buffer Allocation in libCURL-memory saving and libcurlbuffer
LibCURL is a free and open-source powerful client url transfer library. Supports a wide range of platforms and protocols. The platforms include Windows, Linux, and FreeBSD; the protocols include FTP, HTTP (S), Telnet, DICT, and File. It is thread-safe and supports IPv6. At the same time, it is thread-safe.
The content described in this article should be based on the Libcurl basics and specific knowledge. The official description address is http://curl.haxx.se/libcurl/c. You need to read more examples to use them flexibly.
Generally, libCURL data is downloaded to the buffer through the callback function. Its size is usually fixed, so sometimes less than 1/2 is used, but sometimes overflow is used. Here is a simple and easy-to-use method to save memory and solve overflow. The main idea is to use the list char * template in STL, and copy to a pointer allocated with a certain amount of memory during each callback (note that libCURL provides stream data, so it may not end with 0, so sometimes an error occurs when you use a simple strcpy. I am stuck here many times.) The size is size * nmemb, copy the given data one by one and then push_back to the linked list.
When using this data, you may need random access, but the list does not have this feature. Therefore, you can first calculate the total size of all data, open a large buffer, and store it. Of course, after that, all list and char pointers should be released; otherwise, the memory should be doubled.
The source code is as follows:
#include <iostream>#include <list>#include <curl/curl.h>using namespace std;size_t callback_get_head(void *ptr,size_t size,size_t nmemb,void *userp);list<char*> toview;char startht[256]={0};int main(void){ cout<<"Enter HTTP adress:"; cin>>startht; CURL* dcurl=curl_easy_init(); curl_easy_setopt(dcurl,CURLOPT_URL,startht); curl_easy_setopt(dcurl,CURLOPT_WRITEFUNCTION,callback_get_head); curl_easy_perform(dcurl); for (list<char*>::iterator it=toview.begin();it!=toview.end();++it) cout<<*it; for (list<char*>::iterator it=toview.begin();it!=toview.end();++it) delete[] *it; curl_easy_cleanup(dcurl); return 0;}size_t callback_get_head(void *ptr,size_t size,size_t nmemb,void *userp){ char* temp=new char[size*nmemb+1]; char* ptrtmp=(char*) ptr; for (size_t i=0;i<size*nmemb;++i) temp[i]=ptrtmp[i]; temp[size*nmemb+1]=0; toview.push_back(temp); return size*nmemb;}