C 为什么我的代码会导致分段错误?
任务如下:C 为什么我的代码会导致分段错误?,c,arrays,C,Arrays,任务如下: int intarr_save_binary( intarr_t* ia, const char* filename ) { int returnValue = 0; unsigned int len = ia->len; FILE *f; if( NULL == (f = fopen (filename, "wb") )) { perror( "fopen failed" ); returnValue
int intarr_save_binary( intarr_t* ia, const char* filename )
{
int returnValue = 0;
unsigned int len = ia->len;
FILE *f;
if( NULL == (f = fopen (filename, "wb") ))
{
perror( "fopen failed" );
returnValue = 1;
}
else if ( fwrite ( &len, sizeof(int), 1, f) == 1 )
{ // then write of length successful
if (fwrite (ia->data, sizeof(int), len, f) == len)
{
returnValue = 0; // indicate success
}
else
{ // else, write of data failed
returnValue = 3;
}
}
else
{ // else, failed to write len value to file
returnValue = 4;
}
fclose( f ); // cleanup (writes last buffer to file)
return( returnValue );
}
/*实验6任务A*/
/*
以二进制格式将整个数组保存到名为“filename”的文件中
可由intarr\u load\u binary()加载的文件格式。退换商品
成功时为零,失败时为非零错误代码。数组
长度0应生成包含空数组的输出文件。
*/
/*实验6任务B*/
/*
从名为“filename”的文件中加载一个新数组,该文件是
以前使用intarr\u save\u binary()保存。返回指向
成功时新分配的内存,失败时为空。
*/
对于A,我的代码如下所示:
int intarr_save_binary( intarr_t* ia, const char* filename )
{
int returnValue = 0;
unsigned int len = ia->len;
FILE *f;
if( NULL == (f = fopen (filename, "wb") ))
{
perror( "fopen failed" );
returnValue = 1;
}
else if ( fwrite ( &len, sizeof(int), 1, f) == 1 )
{ // then write of length successful
if (fwrite (ia->data, sizeof(int), len, f) == len)
{
returnValue = 0; // indicate success
}
else
{ // else, write of data failed
returnValue = 3;
}
}
else
{ // else, failed to write len value to file
returnValue = 4;
}
fclose( f ); // cleanup (writes last buffer to file)
return( returnValue );
}
对于B,我的代码如下:
intarr_t* intarr_load_binary( const char* filename )
{
unsigned int len = 0;
FILE *f = NULL;
intarr_t* newia = NULL;
if( NULL == fopen (filename, "rb") )
{ // then, fopen failed
perror( "fopen failed" );
exit( EXIT_FAILURE );
} // end if
// implied else, fopen successful
if( NULL == (newia = malloc (sizeof(intarr_t)))){
perror( "malloc failed" );
fclose(f);
exit( EXIT_FAILURE );
} // end if
// implied else, malloc successful
if( (fread (&len, sizeof(int), 1, f) != 1 ) )
{ // then fread failed
perror( "fread failed" );
fclose(f);
free( newia );
exit( EXIT_FAILURE );
} // end if
// implied else, fread for len successful
newia->len = len;
if( NULL == (newia->data = malloc (len*sizeof(int)) ) )
{ // then malloc failed
perror( "malloc failed" );
fclose(f);
free( newia );
exit( EXIT_FAILURE );
} // end if
// implied else, malloc successful
if( fread( newia->data, sizeof(int), len, f ) != len )
{ // then, fread failed
perror( "fread failed" );
fclose(f);
free(newia->data);
free(newia);
exit( EXIT_FAILURE );
} // end if
// implied else, fread successful
fclose (f);
return newia;
} // end function: intarr_load_binary
谁能告诉我为什么我的代码会导致分段错误?多谢各位 在B的代码中,
NULL
被传递到行中的fread()
if( (fread (&len, sizeof(int), 1, f) != 1 ) )
因此,可能会导致分割错误
要解决此问题,请将从fopen()
返回的文件指针指定给f
:改变 到
还要检查传递给函数的参数是否有效。非常感谢!我已经提交了我的代码,并会让你知道结果。再次感谢你对我的帮助!你真棒!这是错误消息:在tarr_save_二进制文件(NULL,“foo”)中测试角落案例;***分段错误***你认为问题出在哪里?@J.Khelly问题是在取消引用之前,没有检查
ia
是否为NULL
。我是C语言的初学者。因此,我不太确定你的意思,哈哈。你介意再解释一下吗?
if( NULL == (f = fopen (filename, "rb")) )