Jump to content

This is ridiculous.....

- - - - -

  • Please log in to reply
5 replies to this topic

#1
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
This caused to be 'segmentation fault'
i have gone through the code 100x, and asked a friend who has been in the programming world 13+ years, and don't know why this is causing this fault error to appear as the coding seemed to be correct as I logically imagined. This is inpart of my testing purposes to expand my knowledge of C programming.


#include <stdio.h>

#include <stdlib.h>

#include <string.h>


int main()

{	

	typedef struct result

	{

		char ID[8];

		char results[25];

	

	}result_t;


	result_t examList[30];

	FILE*f;

	char line [100];

	int count = 0;

	int k;

	char*item;

		

	f = fopen("exam.txt", "r");

	

	while(fgets(line,100,f))

	{

		printf("%s", line);

		

		item = strtok(line, " ");

		strcpy(examList[count].ID, item);

		

		item = strtok(NULL, " ");

		strcpy(examList[count].results, item);

		count++;

	}

	

	fclose(f);


	for(k = 0; k < count; k++)

	{

	printf("%s", examList[k].ID);	

	}

	return(0);

}


Output:

Quote

u3234325 TFFTTFTFFFFTFTFFFTFTFTTTF
u3434325 TFFFTFTFTTTTTTTTTTFTFTTTF
u3534325 TFTFTFTFTFTTFTFFTTFTFTTTF
u3284325 TFTFTFTFFFTTFTFTFFFTTFTTF
u3984325 TFTFTTTFFFFFFFFFFFFTFTTTF
u3732125 TFFFTFTFFFTTFTFTFFFTTFTTF
u3984325 TFTFTTTFTFTTFTFFTTFTFTTTF
u3944325 TFTFTFTFFFTTFTFFTTFTFTTTF
u3104325 TFTTTTFTTTTFTTTTFTTTTFTTT
u3184325 TFTFTFTFFFTTFTFFTFFTFTTTF
u3944325 TFTFTTTFFFTTFTFFTTFTFTTTF
u3874325 TFTFTFTFTFFTFFTFFTFFTFFTF
u3484325 TFFFTFTFFFFTFFFFTFFFFTFFF
u3294325 TFFFTFTFFFTFTFFFTFTFFFTTF
u3174325 TFFFTFTTFFFTFTTFFFTTFFFTF
u3172432 TFFFTFTFFFTTFFFTTFTFFTTTF
u2312325 TFTFTFTFFFFFTTFTFFTTFTTTF
u3284325 TFFFTFTFFFTFTFFFTFTFFFTFF
u3174325 TFTFTTFTFTTFTFTFTTFTFTTFT
u3284325 TFFFTFTFFFTTFTFFTFFFTTFTF
u3864325 TFTFTFTFFFTFFFTTFTFFTTTTF
u3934325 TFFFTFTFFFTTFTFTFFFTTFTTF
u3957825 TFTFTFTFFFTTTFTFTFTFFFTTF
u3764325 TFFFTFTTFFFTTTFTTFFTTTTTF
u3754325 TFFFTTFFFTFFFTTFFFTTFFFTF
u3844325 TFFFTFFFTTFTFTFFFTFTTTTTF
u3691425 TFFFTFFFTFFFTFFFTFFFTFFFF
u3822125 TFFFTFTFFFTFTFFFTFTFFFTTF
u3291425 TFFFTFFFTFFFTFFFTFFFTFFFF
u2344325 TFTFTFTTTTFFFFFFFTTTTTFTF

Segmentation fault



#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Remember what we have taught you in other posts, a segmentation fault will occur most likely when you are overrunning an array - in other words adding more than the array was declared to hold.

examList[30];
Your output shows exactly 30 test results, and then throws a segmentation fault. This would logically dictate that you are at least adding 30 + 1 (or more,) to the array of 30 size. The last line in exam.txt could be a new line which would cause this result, or it could be that there are too many exam entries.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
Alexander - I am aware of that mate
but this is just as weird, I changed the 30 to 29 and the new line disappeared but the fault error remains. :/
Again, I looked through the code 100x, compared with the other executed code. :S

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Have you confirmed that item contains the data you meant for it to contain? Have you confirmed that item contains data that will fit in the arrays? Finally, have you left enough space in your arrays for the trailing \0 character?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Ah, this case is a little more difficult to find. You are invoking undefined behaviour by storing a string in your structure without NULL termination.

Essentially you store ID without termination and then display it later on. It does not know where the string ID ends, so it runs in to memory past the array. This could display memory in the results structure member until a NUL, or in your case of undefined behaviour is a segmentation fault as it ran past likely your process memory causing a violation.

I would recommend therefor, you:

  • Declare useful definitions, i.e. ID_SIZE, and RESULT_SIZE. You can use these later, i.e. for(i = 0;i < RESULT_SIZE; ...) if required)
  • Declare each string member in your struct with an additional space. i.e. char id[ID_SIZE + 1];
  • Use strncpy, instead of strcpy, i.e.
strncpy(examList[count].ID, item, ID_SIZE);
  • Terminate the string, or create your own wrapper around strcpy to do this for you in one call.

examList[count].ID[ID_SIZE-1] = '\0';


Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas

Alexander said:

[*]Use strncpy, instead of strcpy

That's something I've been preaching a lot lately. Good advice.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users