473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Segmentation Fault in fclose()...why?

VB
Hi,

here File.cpp and File.h:
File.cpp:

----------------------
#pragma warning (disable: 4786)

#include <cstring>
#include <cstdlib>
#include <cassert>
#include "File.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

File::File(const char* filename, bool ld)
{
fn_in = new char[strlen(filename)+1];
std::strcpy(fn_in,filename);

load = ld;
maxcnt = isetsz = 0;
curr = reccnt = 0;
// bufsid = -1;
}

File::~File()
{
if (file)
std::fclose(file);
if (fn_in)
delete[] fn_in;
}

bool File::read(ITEMSET& s)
{
if (curr == isetsz) { /* if all records read */
curr = 0; /* reset position of current pointer */
if (!load) /* if to work on input file */
if(!std::fseek(file, pos, SEEK_SET))
error(E_FREAD,fn_in); /* reset file position */
return false; /* return no file read */
}

if (load) /* if to work on memory */
cis = isets[curr]; /* get current itemset */
else
get_iset(); /* otherwhise read from file */
curr++; /* and increment the counter */

s = cis; /* return s */
return true;
}
ITEMSET* File::read()
{
if (curr == isetsz) { /* if all records read */
curr = 0; /* reset position of current pointer */
if (!load) /* if to work on input file */
if(std::fseek(file, pos, SEEK_SET) != 0)
error(E_FREAD,fn_in); /* reset file position */
return NULL; /* return no file read */
}

if (load) /* if to work on memory */
cis = isets[curr]; /* get current itemset */
else {
get_iset(); /* otherwhise read from file */
cis.prepare(); /* sort and remove duplicates */
}
curr++; /* and increment the counter */

return &cis; /* return s */

}
int File::reorg(long thresh)
{
SymTab tmap; /* temporary symbol table */
int i, size;
ITEM* item;
std::vector<ITEM*> sorted = nimap.bvec; /* bucket vector */
std::sort(sorted.begin(),sorted.end(),frqcmp()); /* order by
frequency */

for (i = 0, size = sorted.size();
(i < size) && (sorted[i]->frq >= thresh); i++)
{
item = (ITEM*)tmap.insert(nimap.ids[sorted[i]->id], sizeof(ITEM));
if (!item)
return E_NOMEM; /* add the new item to the map, */
item->frq = sorted[i]->frq;
}
nimap = tmap;

return 0;
}

int File::reset()
{ /* resets to the beginning */
if (load) /* if to work on memory */
curr = 0; /* set the current pointer to 0 */
else {
if(std::fseek(file, pos, SEEK_SET) != 0) /* otherwise reset the file
pointer
*/
error(E_FREAD,fn_in); /* checking for I/O errors */
}
return 0; /* return OK */
}

ITEMSET* File::read(long i)
{ /* read i-th record */
if (i < isetsz) { /* if current record exist */
if (!load){ /* if to work on input file */
int locpos = ftell(file); /* get the local record position */
if(std::fseek(file, pos, SEEK_SET) != 0) /* set the file pointer
*/
error(E_FREAD,fn_in); /* checking for I/O errors */
long cnt = 0; /* get the local record counter */
while(read()&&(++pos<i)); /* read records in sequence until ith */
if(fseek(file, locpos, SEEK_SET) != 0) /* reset the file pointer
*/
error(E_FREAD,fn_in); /* checking for I/O errors */
}
else cis = isets[i];
return &cis; /* return s */
}
else return NULL; /* return no file read */
}

bool File::load_iset(int i, ITEMSET &trans)
{
if (i < isetsz) { /* if current record exist */
if (!load){ /* if to work on input file */
int locpos = ftell(file); /* get the local record position */
if(std::fseek(file, pos, SEEK_SET) != 0) /* set the file pointer */
error(E_FREAD,fn_in); /* checking for I/O errors */
long cnt = 0; /* get the local record counter */
while(read()&&(++pos<i)); /* read records in sequence until ith */
if(std::fseek(file, locpos, SEEK_SET) != 0) /* reset the file
pointer */
error(E_FREAD,fn_in); /* checking for I/O errors */
} else
trans = isets[i];

return true; /* return s */

} else
return false; /* return no file read */

trans=isets[i];
}

char* File::readTrans(int trans)
{
int l = 0;
int i;
for (i = 0; i < isets[trans].size(); i++) {
l += std::strlen(lookup(isets[trans][i]->id)) + 1;
}
char* result = new char[l + 1];
strcpy(result,"");
for (i = 0; i < isets[trans].size(); i++) {
std::strcat(result,lookup(isets[trans][i]->id));
std::strcat(result," ");
}

//fn_in = new char[strlen(filename)+1];
//strcpy(fn_in,filename);
return result;
}


----------------------

Here file.h:

------------------------------------

#ifndef FILE_HPP_INCLUDED
#define FILE_HPP_INCLUDED

#include <vector>
#include "resources.h"
#include "Ctfscan.h"
#include "SymTab.h"

#define BUFSIZE 256 /* size of read buffer */

class File
{
public:
char* readTrans(int trans);

ITEMSET* read(long i);
int reset();
bool loaded() const;
int items();

hash_map<int, int> classcount;
hash_map<std::string, int, hashString, eqstr> classes; /*
Transaction-class
mapping */
ITEMSET* read();
SymTab& symbols();
void insert_iset(ITEMSET is){isets.push_back(is);};
bool load_iset(int i, ITEMSET &trans);
const char* lookup(int id) const;
const char* looktr(int id) const;
int reorg(long thresh);
int size() const;
bool read(ITEMSET& s); /* read an itemset from buffer */
File(const char* fname, /* file name */
bool load = true /* flag for loading item sets */
);
virtual ~File();
protected:
FILE* file; /* file pointer */
SymTab nimap; /* name/identifier map */
SymTab trmap; /* Transaction map */
private:
virtual void read_blk() = 0; /* caches the dataset into memory */
virtual int get_item () = 0; /* --- read an item */
virtual int get_iset () = 0; /* --- read an item set */
// void prepare (); /* --- sort set and remove duplicates */
protected:
std::vector<ITEMSET> isets; /* item set vector */
int isetsz; /* number of itemsets */
ITEMSET cis; /* current item set */
int reccnt; /* number of records read */
bool load; /* flag for file-buffering */
int maxcnt; /* maximal number of items per set */
int curr; /* current record read */
int pos; /* starting reading position in file */
char* fn_in; /* file name */
};

inline int File::items()
{
return nimap.size();
}

inline bool File::loaded()
const{
return load;
}

inline int File::size() const{ /* get the number of itemset */
return isetsz; /* currently loaded */
}

inline const char* File::lookup(int id) /* get the name of a given
identifier*/
const{
return nimap.lookup(id);

}

inline const char* File::looktr(int id) /* get the name of a given
transaction*/
const{
return trmap.lookup(id);

}

inline SymTab& File::symbols() /* gets the symbol table */
{
return nimap;
}

#endif

--------------------------------------

I have a problem at runtime in

File::~File()
{
if (file)
std::fclose(file);
if (fn_in)
delete[] fn_in;
}

Segmentation fault when std::fclose(file) is executed, I don't
understand why...

Here some details:

----------

(gdb) #0 0x00000000 in ?? ()
(gdb) #1 0x400e710d in fclose@@GLIBC_2.1 () from /lib/libc.so.6
(gdb) #2 0x0804b86a in File::~File (this=0x8072c18, __in_chrg=0) at
.../File.cpp:26
(gdb) #3 0x0805bca2 in AsciiFile::~AsciiFile (this=0x8072c18,
__in_chrg=3)
(gdb) at ../AsciiFile.h:19

----------------------------------------

I remember to you that it is a Linux porting from a Win32 version, no
problems there.

Thanks to everybody,

V.B.
Italy
Jul 22 '05 #1
3 6545
VB wrote:
[snip]
Segmentation fault when std::fclose(file) is executed, I don't
understand why...


I didn't run your program and didn't analyze it. But:
A segmentation fault is in 99% of all cases an indication
that you somewhere screewed up the memory management. Either
by using a 'pointer in the wood' or by accessing an array out
of bounds.
Anyway, it is often the case, that the point where the segmentation
fault is recorded is not identical to where the real problem is
located. What you see are just the symptoms, not the cause.

Strategy: Remove everything from the program and start minimal.
(eg. by commenting lots of code)
Does it run?
If yes, then add more code to it.
Do this until the problem shows up again.
A good guess is then, that the real cause is somehow related to
the code section you activated last.

Good luck. Things like that are hard to debug.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
> File::File(const char* filename, bool ld)
{
fn_in = new char[strlen(filename)+1];
std::strcpy(fn_in,filename);

load = ld;
maxcnt = isetsz = 0;
curr = reccnt = 0;
// bufsid = -1;
}

File::~File()
{
if (file)
std::fclose(file);
if (fn_in)
delete[] fn_in;
}

Segmentation fault when std::fclose(file) is executed, I don't
understand why...


You haven't given a compilable code example, so it's hard for anyone
to reproduce your results. Bot one thing does stand out: you
never initialize 'file'. So if you create and then destroy a
File object then you will call fclose with an uninitialized
pointer (which could cause a segfault).

BTW you would be better off to use a std::string for fn_in,
instead of using new/delete.

Jul 22 '05 #3
Old Wolf wrote:
d why...


You haven't given a compilable code example, so it's hard for anyone
to reproduce your results. Bot one thing does stand out: you
never initialize 'file'. So if you create and then destroy a
File object then you will call fclose with an uninitialized
pointer (which could cause a segfault).

Further, there's all this owned object (pointers and the FILE*)
managed by the constructor and destructor, yet there's no copy constructor
and copy assignment operator.

The whole thing is a very bad design.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Atulvid | last post by:
Hi, I created a test file for my application by writing structures to a binary file. To make sure that I will get correct data for the pointers inside the structure, i wrote actual data they...
3
by: Anks | last post by:
i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as...
16
by: Glen | last post by:
i get segmentation fault when i execute the following code(the platform is gnu/linux) #include<stdio.h> int main() { char fn1,fn2,tn; printf("\nEnter the first file name :");...
10
by: F?bio Botelho | last post by:
Sorry About the english.... This program copy one file to another , but when i run it it's gives-me an error: Segmentation fault I don't understand ... because it pass my test : opens the...
19
by: Sameer | last post by:
Hi friends, I am using Mandriva Linux 9.2 and gcc. My source code is, int chunkin ; //no error int i ; for (i=0;i<7225;i++) { chunkin = somedata ;
11
satyanagendra
by: satyanagendra | last post by:
hi I already write a database program in c and it is working fine. I change some modifications in the structure which holds the data in the database in that i got segmentation fault. the code is...
5
by: lancer6238 | last post by:
Dear all, I'm trying to implement the Smith-Waterman algorithm for DNA sequence alignment. I'm aligning a 2040 bp query sequence against a 5040 bp sequence. I'll be trying to implement a parallel...
8
by: parkskier | last post by:
Ok, so I got a segmentation fault when I tried to run the program I created. I understand what this error means, but I don't know where it is. If someone can quickly look through my code and point...
6
by: DrSchwartz | last post by:
Hi all, I'm completely stuck.. I have a program which reads integers (there are 65536 integers) from one file, and adds them into the Bloom filter. And then it reads another file (with a larger set...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.