LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-06-2009, 10:53 PM   #1
nishidh
LQ Newbie
 
Registered: Jan 2009
Posts: 1

Rep: Reputation: 0
"Segmentation Fault" found when execute the following code....


The below file is header.h file:

#include <stdlib.h>
#include <math.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>


#define STRBUF 256 /* Default string buffer size */
#define SEPARATOR(a) (a == ' ' || a == ',') ? 1 : 0
#define SUCCESS(a) if(a == NULL) { printf("\nUnsuccessful Allocation");\
exit(0);}

#define HASHTBLSIZE 101


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;


The below file is lib.c

#include "header.h"

#define IB1 1 /* Definitions for GetChangedSeed() */
#define IB2 2
#define IB5 16
#define IB18 131072

#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 */

GetDataIntoBuffer( filename );
}
/*---------------------------------------------------------------------------*/
long GetNumberofTuples(char *filename){
FILE *pfp;
char command[STRBUF];

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){

DataSet = malloc(sizeof(char*) * row);
SUCCESS( DataSet );
while(row){
DataSet[ row - 1] = (char *)malloc(sizeof(char) * col * 6);
SUCCESS( DataSet[ row - 1 ] );
row --;
}
}
/*---------------------------------------------------------------------------*/
void GetDataIntoBuffer(char *database){
FILE *fp;
long tuples, field, i, j;
char *buffer;


fp = fopen (database, "r");
buffer = malloc(COL * 6);
for (tuples = 0; tuples < ROW; tuples ++){
fgets( buffer, COL * 6, fp); /* collect one tuple */
strcpy(DataSet[tuples], buffer);
}
fclose (fp);
free(buffer);
}
/*---------------------------------------------------------------------------*/
void FreeDataBuffer(){

free( DataSet );
}
/*---------------------------------------------------------------------------*/
void DisplayDataBuffer(){
int i = ROW;
int j = COL;

for(i = 0 ; i < ROW; i ++)
printf("%s", DataSet[i]);
}

The below file is rand.c:

#include "header.h"

/***********************External Definitions*******************************/
extern long ROW, COL;
extern char **DataSet;
extern void ReadData(char *);
extern long GetRandomNumber(long);

void CreateFile(char*);
void RandomizeData(long);
void Swap(long, long);
/***************************************************************************/

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]);
ReadData(file);
RandomizeData(N);
CreateFile(target);
}
/***************************************************************************/
void RandomizeData(long N){
long times = N * ROW, i;
long indx1, indx2;
for(i = 0; i < times; i++){
indx1 = GetRandomNumber( ROW - 1);
if( indx1 > ROW - 1 )
indx1 = ROW - 1;
if( indx1 < 0 )
indx1 = 0;
indx2 = GetRandomNumber( ROW - 1);
if( indx2 > ROW - 1 )
indx2 = ROW - 1;
if( indx2 < 0 )
indx2 = 0;
Swap(indx1, indx2);
}
}
/***************************************************************************/
void Swap(long indx1, long indx2){
long i;
char *buffer;

buffer = (char*)malloc(COL * 6);
strcpy(buffer, DataSet[indx1]);
strcpy(DataSet[indx1], DataSet[indx2]);
strcpy(DataSet[indx2], buffer);
free(buffer);
}
/***************************************************************************/
void CreateFile(char *filename){
FILE *fp;
long i;

fp = fopen(filename, "w");

for ( i = 0; i < ROW; i++)
fputs(DataSet[i], fp);
fclose(fp);
}

The below is Makefile which is executed when I use make command:

exe: rand.o lib.o
gcc -o shuffle rand.o lib.o
clean:
rm -f *.o shuffle

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.

Tahnks in advance.
 
Old 01-07-2009, 03:41 PM   #2
Gethyn
Member
 
Registered: Aug 2003
Location: UK
Distribution: (X)Ubuntu 10.04/10.10, Debian 5, CentOS 5
Posts: 900

Rep: Reputation: 32
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...
 
Old 01-21-2009, 03:50 PM   #3
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
To include the debugging symbols to get gdb working, then add the option -g to your gcc command, i.e.
Code:
gcc -g -o shuffle rand.o lib.o
 
Old 01-21-2009, 04:11 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by nishidh View Post
Code:
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.
 
Old 01-21-2009, 04:53 PM   #5
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Linux 11 (Bullseye)
Posts: 3,407

Rep: Reputation: 141Reputation: 141
I don't think the OP is here anymore. The assignment must have been due, already.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
5.2. Toolchain Technical Notes "segmentation fault" on ALL bz2 Majin_Buu Linux From Scratch 0 08-20-2008 04:39 AM
Dosemu Error "segmentation fault" on fedora core 7 tritonw Linux - Software 1 01-04-2008 02:10 PM
sunbird 0.3 does not start, error: Segmentation fault : "$prog" ${1+"$@"} polemon Ubuntu 8 01-08-2007 04:22 AM
when code tries to execute at that time i am getting segmentation fault dayalan_cse Programming 1 12-17-2006 10:30 AM
why iam getting problems like "...too many files opened" or "segmentation fault" naren_0101bits Linux - Newbie 2 07-19-2004 12:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration