问题描述
- solaris下编译的程序执行是出现段错误
- 编译器版本
Reading specs from /usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/specs
Configured with: /sfw10/builds/build/sfw10-patch/usr/src/cmd/gcc/gcc-3.4.3/configure --prefix=/usr/sfw--with-as=/usr/ccs/bin/as --without-gnu-as --with-ld=/usr/ccs/bin/ld --without-gnu-ld --enable-
languages=cc++ --enable-shared
Thread model: posix
gcc version 3.4.3 (csl-sol210-3_4-branch+sol_rpath)test.h:
#include
#include
#include
#include
#include
#include
#include
#define MAX_DATE_STR_LEN 30
#define SECONDS_PER_DAY 3600 * 24
#define PATH_TOKEN ""/""
#define FILE_TYPE_DIR 0
#define FILE_TYPE_OTHER 1
#define FILE_TYPE_NORMAL_FILE 2
int const FILETYPECOUNT=6 ;
using namespace std;
using std::vector;
using std::string;
struct FileInfoSole{
string Name;
string Path;
string FullPath;
long FileId;
int Result;
int Reason;
string ReasonDesc;
int Type;
long Size;
string CreateTime;
string ErrFile;
string TmpErrFile;
string FileType;
};
typedef vector FileListSole;test.cpp:
#include ""test.h""
using namespace std;
int GetFileListSole(string dir FileListSole &fileListstring substr int flag);
string tttypes[FILETYPECOUNT]={""CUST""USER""RED""HT""LT""MH""};
int main(){
string dir=""./so/"";
FileListSole fileList;
string substr;
GetFileListSole(dirfileListsubstr0);
return 0;
}
int GetFileListSole(string dir FileListSole &fileListstring substr int flag){
DIR *pDir = NULL;
if ( ( pDir = opendir(dir.c_str()) ) == NULL ){
return false;
}
int retCode = 0;
FileInfoSole fileInfoSole;
//int solearr[200];
cout<<""link sole""<<endl;
cout<<fileInfoSole.Name<<endl;
cout<<fileInfoSole.Name<<endl;
cout<<fileInfoSole.Name<<endl;
struct dirent entry;
struct dirent *result;
cout<<""4""<<fileInfoSole.Name<<endl;
//for( retCode = readdir_r( pDir &entry &result ); result != NULL && 0 == retCode; retCode =readdir_r( pDir &entry &result ) )
retCode = readdir_r( pDir &entry &result );
for(int i=0 ;i< 4;i++)
{
retCode = readdir_r( pDir &entry &result );
cout<<""5""<<fileInfoSole.Name<<endl;//循环第一次时,程序在这个地方蹦会了,出现段错误,郁闷彻底,求大侠帮助
if( strcmp( entry.d_name."" ) == 0 ||strcmp( entry.d_name.."") == 0)
{
continue;
}
cout<<""testa""<<endl;
cout<<fileInfoSole.Name<<endl;
cout<<""---""<<endl;
fileInfoSole.Name = entry.d_name;
// cout<<fileInfoSole.Name<<endl;
cout<<""testb""<<endl;
if (substr!="""")
{
if (fileInfoSole.Name.find(substr)==string::npos)
{
//printf(""%sn""fileInfoSole.Name.c_str());
continue;
}
else
{
//printf(""%sn""fileInfoSole.Name.c_str());
fileInfoSole.FileType=substr;
}
}else
{
fileInfoSole.FileType=string("""");
for(int k=0;k<FILETYPECOUNT;k++)
{
if (fileInfoSole.Name.find(tttypes[k])!=string::npos)
{
fileInfoSole.FileType=string(tttypes[k]);
break;
}
}
}
//printf(""%sn""fileInfoSole.Name.c_str());
fileInfoSole.Path = dir;
fileInfoSole.FullPath.clear();
fileInfoSole.FullPath.append(dir).append(PATH_TOKEN).append(entry.d_name);//GetFileInfo( fileInfoSole ); if ( FILE_TYPE_NORMAL_FILE == fileInfoSole.Type ){ //fileList.push_back(fileInfoSole); } if ( FILE_TYPE_DIR == fileInfoSole.Type ) { if (flag) { GetFileListSole(fileInfoSole.FullPathfileListsubstr flag); } }}closedir(pDir);cout<<""over""<<endl;return true;
}
so文件夹:
-bash-3.2$ ls -l so
total 0
-rw-r--r-- 1 bill bill 0 Apr 10 18:30 fileiiiiiiiiiiiiii
执行程序:
-bash-3.2$ ./a.out
link sole4
5
Segmentation Fault (core dumped)
-bash-3.2$
我就想问程序到cout<<""5""<<fileInfoSole.Name<<endl;个语句是报段错误,我猜测是系统调用导致的内存泄露,于是我在程序
FileInfoSole fileInfoSole;和struct dirent entry; 之间放了一个int solearr[200];
数组,就不会出现段错误了或者把so文件夹下的文件""fileiiiiiiiiiiiiii""改成短一点的""file""的时就不会出现段错误了,请问这是什么原因导致的,有什么更好的办法解决这个问题,越详细细越好,等代大神解决.....