1

Problem: When i tried to compile my file and run, it has segmentation problem. When I passed the file to my friend (he is using the same version of ubuntu), the server will be able to run. I am wondering why?

Below will be my code for the whole page. Personally I feel that there is no problem with it but I will just paste it for reference in case anyone asks for it.

void readNStoreData () { char words[MAX]; char *wholeLine; char* delimiter = ","; int cflag = 0; int x, count = 0; char input; FILE *countryFile; countryFile = fopen("Countries.txt","r"); if (!countryFile) { exit(EXIT_FAILURE); } while (fgets (words, MAX - 1, countryFile) != NULL) { //atof to convert string to double //split a single line into individual tokens indicating , as the delimeter //afterwards store them into array wholeLine = strtok (words, delimiter); strcpy (records [count].TDL, wholeLine); wholeLine = strtok (NULL, ","); strcpy (records [count].cName, wholeLine); wholeLine = strtok (NULL, delimiter); strcpy (records [count].FIPS104, wholeLine); wholeLine = strtok (NULL, delimiter); strcpy (records [count].ISO2, wholeLine); wholeLine = strtok (NULL, delimiter); strcpy (records [count].ISO3, wholeLine); wholeLine = strtok (NULL, delimiter); records [count].ISO = atof(wholeLine); wholeLine = strtok (NULL, delimiter); strcpy (records [count].cCapital, wholeLine); wholeLine = strtok (NULL, delimiter); strcpy (records [count].cRegion, wholeLine); wholeLine = strtok (NULL, delimiter); strcpy (records [count].cCurrencyName, wholeLine); wholeLine = strtok (NULL, delimiter); strcpy (records [count].cCurrencyCode, wholeLine); wholeLine = strtok (NULL, delimiter); records [count].cPopulation = atof(wholeLine); count++; } fclose(countryFile); //close file } 

I hope someone will be able to spot the mistake somewhere. Thanks to advance to those who helped!

run the gdb and the error is actually this line. it is situated in this function.

 l(gdb) frame 1 l#1 0x08048936 in readNStoreData () at testserver.c:61 61 strcpy (records [count].cName, wholeLine); 
2
  • 3
    Did you try to debug with valgrind, gdb, perhaps strace? Do you know on what line the error occurs? I'm not going to debug this for you, this is horrible code, why did you use strcat like that if you know about sprintf? Commented Feb 9, 2012 at 13:22
  • @cha0site i have debugged and here is the error. Commented Feb 9, 2012 at 15:18

2 Answers 2

2

I strongly suggest you to learn to use a debugger, such as GDB. You can install it on ubuntu by sudo apt-get install gdb

Here is a short tutorial.

Google finds many more examples

EDIT:

Since you now have GDB running, try setting a breakpoint before run:

(gdb) br testserver.c:61

and after you do run you should be able to print the various variables and see which one is illegal.

Sign up to request clarification or add additional context in comments.

4 Comments

(gdb) frame 1 #1 0x08048936 in readNStoreData () at testserver.c:61 61 strcpy (records [count].cName, wholeLine);
@Andres: what are you posting it for? It is your answer :) That is a buffer overrun or corrupted src/dest pointers.
@sehe, to be frank i dont really know what can i do with it. and what i should do to make it right. any comments?
@Andres: the call to strcpy fails. Simply check all arguments to strcpy. Here is the man page: opengroup.org/sud/sud1/xsh/strcpy.htm
0

Your call to strcpy fails. You pass bad parameters into it (see http://www.opengroup.org/sud/sud1/xsh/strcpy.htm for documentation).

If you want us to analyze this, you need to show us

  • how records is defined,
  • what the element struct type is defined like,
  • how you initialize the records array, to how many elements,
  • how you limit count to not go beyond the capacity of the records array
  • how you make sure that records[count].cName (presumably either char* or char[]) is large enough to contain the token parsed by strtok

    Note The largest possible token is MAX characters, since it could be the MAX linelength returned by fgets + 1 trailing NUL character iff there isn't a single delimiter character in the input.

This should get you underway.

I have a sneaking suspicion that you may have forgotten to initialize the receiving array (records) alltogether, but again, we can't know unless you show more code.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.