fwrite()和free()错误 [英] fwrite() and free() bug
问题描述
您好,
我对此代码有一个相当奇怪的错误:对于''buf''的某些值,
a分段错误当''free(buf)'后跟''fwrite()''时发生。
在程序输出中,''perror()''没有报告错误
文件写得正确。
/ *失败时返回NULL,成功时返回calloc()ed数据* /
char * recv_data(int * bytes_read);
int main(int argc,char ** argv)
{
char * buf;
int bufLen;
FILE * fd;
/ * ...一些初始化代码* /
buf = recv_data(& bufLen); / *''bufLen''的长度为''buf''* /
if(buf!= NULL)
{
if(fwrite(buf,sizeof(char),bufLen,fd)< = 0)
perror(" fwrite");
free(buf); / *分段错误发生在这里* /
buf = NULL;
}
返回0;
}
当我删除对''fwrite()''的调用时没有崩溃,无论''buf'的
值如何。因此我猜想''fwrite()''是罪魁祸首。
有什么想法吗?
谢谢。
" Suraj Kurapati" < SK ****** @ ucsc.edu>在消息中写道
新闻:40abe3dc @ darkstar ...你好,
我对这段代码有一个相当奇怪的错误:对于
''buf''的某些值,当''free(buf)'后跟''fwrite()''时会发生分段错误。
在程序输出中,有''perror()''报告没有错误,
文件写得正确。
/ *失败时返回NULL,成功时返回calloc()ed数据* /
char * recv_data(int * bytes_read);
int main(int argc,char ** argv)
{* char * buf;
int bufLen;
FILE * fd;
/ * ...一些初始化代码* /
buf = recv_data(& bufLen); / *''bufLen''的长度为
''buf''* /
如果(buf!= NULL)
{
if(fwrite(buf,sizeof( char),bufLen,fd)< = 0)
perror(" fwrite");
free(buf); / *分段错误发生在这里* /
buf = NULL;
}
返回0;
}
没有崩溃的时候我删除了对''fwrite()''的调用,无论''buf''的值是多少。因此我推测''fwrite()''是罪魁祸首。
我猜想buf没有分配空间。
>
-
Mabden
Mabden写道:我猜想buf没有分配空间实际上,这个条件由语句if(buf!= NULL){}来检查。
考虑一下:
if(buf!= NULL)
{
free(buf); / *工作正常* /
}
与此相反:
if(buf!= NULL )
{
if(ffrite(buf,sizeof(char),bufLen,fd)< = 0)
perror(" fwrite");
free(buf); / *分段错误发生在这里* /
}
谢谢。
在文章< 40abeaec @ darkstar> ;, Suraj Kurapati< sk ****** @ ucsc.edu>写道:Mabden写道:
实际上,这个条件由语句if(buf!= NULL){}进行检查。
当然,这并没有检查你的功能是否正确分配了空间
。
显然有些东西继续你没有在代码中显示你已经发布的b $ b。其他地方可能是malloc()/ free()错误。我建议将
链接到malloc()的调试版本。
fwrite()本身可能会调用malloc(),这可以解释为什么
错误只在你打电话时出现。
- Richard
Hello,
I''m having a rather strange bug with this code: for certain values of ''buf'',
a segmentation fault occurs when ''free(buf)'' is followed by an ''fwrite()''.
In the program output, there is no error reported by ''perror()'' and the
file is written correctly.
/* returns NULL upon failure, or a calloc()ed data upon success */
char* recv_data(int *bytes_read);
int main(int argc, char** argv)
{
char *buf;
int bufLen;
FILE *fd;
/* ... some initialization code */
buf = recv_data(&bufLen); /* ''bufLen'' has the length of ''buf'' */
if(buf != NULL)
{
if(fwrite(buf, sizeof(char), bufLen, fd) <= 0)
perror("fwrite");
free(buf); /* segmentation fault occurs here */
buf = NULL;
}
return 0;
}
There is no crash when I remove the call to ''fwrite()'', regardless of the
value of ''buf''. Thus I conjecture that ''fwrite()'' is the culprit here.
Any ideas?
Thanks.解决方案"Suraj Kurapati" <sk******@ucsc.edu> wrote in message
news:40abe3dc@darkstar...Hello,
I''m having a rather strange bug with this code: for certain values of ''buf'', a segmentation fault occurs when ''free(buf)'' is followed by an ''fwrite()''.
In the program output, there is no error reported by ''perror()'' and the
file is written correctly.
/* returns NULL upon failure, or a calloc()ed data upon success */
char* recv_data(int *bytes_read);
int main(int argc, char** argv)
{
char *buf;
int bufLen;
FILE *fd;
/* ... some initialization code */
buf = recv_data(&bufLen); /* ''bufLen'' has the length of ''buf'' */
if(buf != NULL)
{
if(fwrite(buf, sizeof(char), bufLen, fd) <= 0)
perror("fwrite");
free(buf); /* segmentation fault occurs here */
buf = NULL;
}
return 0;
}
There is no crash when I remove the call to ''fwrite()'', regardless of the
value of ''buf''. Thus I conjecture that ''fwrite()'' is the culprit here.
I conjecture that buf has no space allocated to it.
--
Mabden
Mabden wrote:I conjecture that buf has no space allocated to it.
Actually, this condition is checked by the statement "if(buf != NULL) {}".
Consider this:
if(buf != NULL)
{
free(buf); /* works fine */
}
As opposed to this:
if(buf != NULL)
{
if(fwrite(buf, sizeof(char), bufLen, fd) <= 0)
perror("fwrite");
free(buf); /* segmentation fault occurs here */
}
Thanks.
In article <40abeaec@darkstar>, Suraj Kurapati <sk******@ucsc.edu> wrote:Mabden wrote: Actually, this condition is checked by the statement "if(buf != NULL) {}".
That doesn''t check that your function really allocated the space
correctly, of course.
There''s clearly something going on that''s not shown in the code you''ve
posted. Probably a malloc()/free() error somewhere else. I suggest
linking with a debugging version of malloc().
fwrite() itself may well call malloc(), which would explain why the
error only shows up when you call it.
-- Richard
这篇关于fwrite()和free()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!