• Advertisement
Sign in to follow this  

Segmentation Fault Before Deconstructor

This topic is 2680 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Environment: Mingw-5.14 (GCC 4.5.0)

I'm getting a Seg Fault using this code.


Header File
class DPAC_Header
{
ifstream *In;
string Signature;
unsigned long ListOfs; //Should always be 0x100
unsigned long NumDList;
unsigned long DListSiz;
unsigned long DirSize;
unsigned long NumDListBlk;
public:
DPAC_Header();
DPAC_Header(string Filename);
DPAC_Header(ifstream *File);
~DPAC_Header();
unsigned long GetListOfs();
unsigned long GetNumDLst();
unsigned long GetDataOfs();
};


Implemantation File

DPAC_Header::DPAC_Header()
{
In = new ifstream;
Signature = "DPAC";
ListOfs = 0x100;
NumDList = 0;
DListSiz = 0;
DirSize = 0;
NumDListBlk = 1;
}


DPAC_Header::DPAC_Header(string Filename)
{
In = new ifstream(Filename.c_str(),ios::binary);
if(In->good())
{
In->read((char*)(&Signature),4);
In->read((char*)(&ListOfs),sizeof(ListOfs));
In->read((char*)(&NumDList),sizeof(NumDList));
In->read((char*)(&DListSiz),sizeof(DListSiz));
In->read((char*)(&DirSize),sizeof(DirSize));
In->read((char*)(&NumDListBlk),sizeof(NumDListBlk));
}
}

DPAC_Header::DPAC_Header(ifstream *File)
{
In = File;
if(In->good())
{
In->read((char*)(&Signature),4);
In->read((char*)(&ListOfs),sizeof(long));
In->read((char*)(&NumDList),sizeof(long));
In->read((char*)(&DListSiz),sizeof(long));
In->read((char*)(&DirSize),sizeof(long));
In->read((char*)(&NumDListBlk),sizeof(long));
}
}

Executable File

int main()
{
ifstream Fname("C.PAC",ios::binary);
DPAC_Header Head(&Fname);
cout << Head.GetListOfs() << endl;
cout << Head.GetNumDLst() << endl;
return 0;
}


Using the debugger, the segmentation fault is created before the deconstructor. I have no idea what it could be about.

Share this post


Link to post
Share on other sites
Advertisement
You can't read and write std::string objects just by casting the address. You might want to read this.

Share this post


Link to post
Share on other sites
Thank you for the information. I now know how stupid that was. I just wished I knew how to keep from doing this:


char Temp[4];
In->read((char*)(Temp),4);
Signature = Temp;

Share this post


Link to post
Share on other sites
You could just pack up the whole header so that it byte-for-byte will always match, then read/write the whole header in one go.


#pragma pack(push,1)
struct Header
{
char Signature[4];
unsigned long ListOfs; //Should always be 0x100
unsigned long NumDList;
unsigned long DListSiz;
unsigned long DirSize;
unsigned long NumDListBlk;
}
#pragma pack(pop,1)

ifstream ifile("foo");
Header header;
ifile.read( &header, sizeof(header) );
ifile.close();




You also need to watch out (in my sample and your sample) for Endianness. If you read the files on a different platform than you wrote them, you may need to swap the bytes around (not all processors are little-endian like the x86). You may also need to worry about 32 vs 64 bit compilers changing the size of the long on you. You should probably use the types like uint32_t or uint64_t for your data.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement