realloc():下一个大小无效

realloc():下一个大小无效,c,libcurl,realloc,C,Libcurl,Realloc,我对realloc函数有问题。我只对LibCurl使用C(所以没有向量)。我遇到的问题是,在write_data函数(我作为回调传递给Curl的函数)的第12次迭代中,我得到了以下错误(realloc():下一个大小无效),每次libcurl有一些数据要传递回来时(数据以块的形式传递)都会调用它 跟踪: -除去- 资料来源: #包括 #包括 #包括 #包括 字符*数据//存储数据 尺寸\u t运行尺寸; int写入数据(字符*ptr、大小\u t大小、大小\u t nmemb、空*流) { si

我对realloc函数有问题。我只对LibCurl使用C(所以没有向量)。我遇到的问题是,在write_data函数(我作为回调传递给Curl的函数)的第12次迭代中,我得到了以下错误(realloc():下一个大小无效),每次libcurl有一些数据要传递回来时(数据以块的形式传递)都会调用它

跟踪: -除去-

资料来源:
#包括
#包括
#包括
#包括
字符*数据//存储数据
尺寸\u t运行尺寸;
int写入数据(字符*ptr、大小\u t大小、大小\u t nmemb、空*流)
{
size_t ThisSize=(size*nmemb);//存储要存储的数据的大小
size_t DataLen=strlen(Data);//到目前为止数据的长度
RunningSize=(RunningSize+ThisSize);//更新运行大小(用作新大小)
Data=realloc(Data,RunningSize);//获取新的mem位置(在第12次迭代中,此操作失败)
strcat(Data,ptr);//将ptr中的数据添加到数据
return ThisSize;//函数必须返回它接收到的数据的大小,以便cURL知道一切正常。
}
int main()
{
卷曲*卷曲;
卷曲编码;
const char*UserAgent=“”;
Data=malloc(1);//所以realloc可以工作
RunningSize+=1;
curl=curl_easy_init();
if(curl)
{
curl_easy_setopt(curl,CURLOPT_NOBODY,0);
curl\u easy\u setopt(curl,CURLOPT\u URL,“http://www.google.co.uk/" );
curl\u easy\u setopt(curl,CURLOPT\u WRITEFUNCTION,write\u data);
curl_easy_setopt(curl,CURLOPT_USERAGENT,USERAGENT);
curl_easy_setopt(curl,CURLOPT_头,1);
//预制件请求。
res=旋度(curl)\u容易执行(curl);
//输出数据(调试目的)
(数据);
//清理
旋度\轻松\清洁(旋度);
免费(数据);
}
返回0;
}

提前感谢,

传递到
write_data()
的数据不一定是nul终止的;这就是它告诉你字节数的原因

这意味着您不能使用strcat()。使用它会从数组的末尾运行,并破坏
malloc
/
realloc
所使用的数据结构,从而导致错误

您的
write_data()
应该使用
memcpy()
,如下所示:

int write_data( char *ptr, size_t size, size_t nmemb, void *stream )
{
    size_t ThisSize = (size * nmemb); //Stores the size of the data to be stored
    size_t DataLen = RunningSize; //length of the data so far

    RunningSize = (RunningSize + ThisSize ); //update running size (used as new size)

    Data = realloc( Data, RunningSize ); //get new mem location (on the 12th iteration, this fails)
    memcpy((char *)Data + DataLen, ptr, ThisSize); //add data in ptr to Data

    return ThisSize; //the function must return the size of the data it received so cURL knows things went ok.
}

您还需要将
RunningSize
初始化为0,而不是1。您可以将
数据
初始化为
NULL
-允许将
NULL
传递到
realloc()
(并使其行为类似于
malloc()
)。

如果不查看源代码,就很难知道表的含义和实际发生的情况。请在您的问题中加入相关的源代码。您发布到
pastebin
中的内容实际上是无用的。您的代码中没有对
写入数据的调用,因此无法确定您在那里做什么。@AndreyT:这是一个回调,从libcurl内部调用。附带说明:无需执行
malloc(1)
来初始化
realloc
的指针
realloc
可以“重新分配”空指针,而不会出现任何问题。
realloc
在正确使用方面稍显灵活。您需要通过检查非NULL返回值来检查它是否成功,但是,如果它确实返回NULL,则仍然需要释放参数指向的内存。因此,如果
x
是指向内存区域的指针的唯一副本,则使用
x=realloc(x,n)
是个坏主意。你需要做(类似于)
new\ux=realloc(x,n);如果(new_x)x=new_x;else失败_动作()非常感谢您的帮助,它工作得非常好!
int write_data( char *ptr, size_t size, size_t nmemb, void *stream )
{
    size_t ThisSize = (size * nmemb); //Stores the size of the data to be stored
    size_t DataLen = RunningSize; //length of the data so far

    RunningSize = (RunningSize + ThisSize ); //update running size (used as new size)

    Data = realloc( Data, RunningSize ); //get new mem location (on the 12th iteration, this fails)
    memcpy((char *)Data + DataLen, ptr, ThisSize); //add data in ptr to Data

    return ThisSize; //the function must return the size of the data it received so cURL knows things went ok.
}