"Segmentation Fault" found when execute the following code....
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
typedef struct tupleclass{
long ClassValue; /* Class of the instance */
long ClassCount; /* Frequency within the class */
struct tupleclass *Next; /* Ptr to the next TUPLECLASS */
}TUPLECLASS;
typedef struct instance{
long tuple; /* Contains index of the instance in DataSet */
long ClassCount; /* Number of distinct class of instance */
struct instance *Down; /* Pointer to the next instance */
TUPLECLASS *Right; /* Pointer to the tuple class */
}INSTANCE;
typedef struct hashtable{
INSTANCE *Down; /* Pointer to the first instance */
long Count; /* Number of distinct instance */
}HASHTABLE;
#define MBIG 1000000000 /* Definitions for rand3() */
#define MSEED 161803398
#define MZ 0
#define FAC (1.0/MBIG)
/*----------------------------- Global variables ----------------------------*/
char **DataSet; /* dataset buffer */
long ROW, COL; /* Number of row and column in a dataset */
unsigned long Seed; /* Seed for random value generation */
/*-------------------------Function references-------------------------------*/
long GetNumberofAttributes(char *);
long GetNumberofTuples(char *);
void AllocateDataSpace( long, long );
void GetDataIntoBuffer(char *);
void DisplayDataBuffer();
/*---------------------------------------------------------------------------*/
void InitRandomize(){
Seed = time(NULL);
}
/*---------------------------------------------------------------------------*/
int GetChangedSeed(unsigned long *seed)
{
unsigned long newbit;
newbit = (*seed & IB18) >> 17 /* Get bit 18 */
^ (*seed & IB5) >> 4 /* XOR with bit 5 */
^ (*seed & IB2) >> 1 /* XOR with bit 2 */
^ (*seed & IB1); /* XOR with bit 1 */
*seed = (*seed << 1) | newbit; /* Leftshit the seed and put the result */
return (int) newbit; /* of the XOR's in its bit 1 */
}
/*---------------------------------------------------------------------------*/
/*---------------------Generates Random feature set--------------------------*/
/*---------------------------------------------------------------------------*/
void GenerateRandomFeatures(char *mask)
{
long i;
for ( i = 0; i < COL - 1; i++ ) {
if( GetChangedSeed( &Seed ) )
mask[ i ] = 1;
else
mask[ i ] = 0;
}
}
/*---------------------------------------------------------------------------*/
float ran3 (idum)
long *idum;
{
static int inext, inextp;
static long ma[56];
static int iff=0;
long mj,mk;
int i,ii,k;
if (*idum < 0 || iff == 0) { /* initialization */
iff=1;
mj=MSEED-(*idum < 0 ? -*idum : *idum);
mj %= MBIG;
ma[55]=mj;
for (i=1;i<=54;i++) {
ii=(21*i) % 55;
ma[ii]=mk;
mk=mj-mk;
if (mk < MZ) mk += MBIG;
mj=ma[ii];
}
for (k=1;k<=4;k++)
for (i=1;i<=55;i++) {
ma[i] -= ma[1+(i+30) % 55];
if (ma[i] < MZ) ma[i] += MBIG;
}
inext=0;
inextp=31;
*idum=11;
}
if (++inext == 56) inext=1;
if (++inextp == 56) inextp=1;
mj=ma[inext]-ma[inextp];
if (mj < MZ) mj +=MBIG;
ma[inext]=mj;
return mj*FAC;
}
/*---------------------------------------------------------------------------*/
/*-------------------------Random Function Generator-------------------------*/
/*---------------------------------------------------------------------------*/
long GetRandomNumber(long MaxRange){
long value;
value = (GetChangedSeed(&Seed) + 1) * 786;
return (long) (ran3(&value) * MaxRange);
}
/*---------------------------------------------------------------------------*/
/*--------------------Reads data from the file-------------------------------*/
/*---------------------------------------------------------------------------*/
ReadData(char *filename){
FILE *fp;
/* checking file existance */
if ((fp = fopen(filename, "r")) == NULL){
printf ("\n %s not found in this path", filename);
exit(0);
}
fclose( fp );
/* get number of tuples and attrib from the data file*/
COL = GetNumberofAttributes( filename );
ROW = GetNumberofTuples( filename );
/* allocate memory for the data set in the DataSet buffer */
AllocateDataSpace( ROW, COL );
/* Collect data into DataSet buffer from the file */
sprintf(command, "cat %s | wc -l", filename); /* use wc system command */
pfp = popen(command, "r"); /* open a pipe to read */
fgets(command, STRBUF, pfp); /* get the number of line */
pclose (pfp);
return atol(command);
}
/*---------------------------------------------------------------------------*/
long GetNumberofAttributes(char *filename){
long i, field;
FILE *fp;
char buffer[STRBUF];
fp = fopen(filename, "r");
fgets( buffer, STRBUF, fp);
fclose (fp);
field = 0;
for ( i = strlen(buffer); i; i--){
if ( SEPARATOR(buffer[i]) )
field ++;
}
return field + 1; /* number of field is = separator + 1 */
}
/*---------------------------------------------------------------------------*/
void AllocateDataSpace(long row, long col){
So when i execute make exe command it generate object file and a command named shuffle but when I try to execute it it will show me the error of "segmentation fault" I execute this code on Fedora.
Pls help me or give me some review where actually the problem is and how to solve it. I try to debug it using gdb but unable to find debug symbol.
That's a lot of code to ask people to look through. Any chance you can make a reduced version of the program that has the same problem? You might also have more luck with this on a coding forum, I doubt the problem is specific to Linux...
main(int argc, char *argv[]){
long N;
char file[256];
char target[256];
N = atol(argv[3]);
strcpy(target, argv[2]);
strcpy(file, argv[1]);
It's never a good idea to trust the command line that totally.
What if there are less than three arguments on the command line? What if the third one isn't a number? Maybe you should even worry that one of the first two is longer than 255 characters.
Maybe this is your seg fault. Run with too few arguments on the command line and atol(argv[3]) will probably seg fault.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.