1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Segmentation Faults in C

Discussion in 'HTML, Graphics & Programming' started by mleanz1, 14 Jan 2010.

  1. mleanz1

    Hitman

    Joined: 28 Feb 2009

    Posts: 519

    Whats the best way of debugging a segmentation fault. I know it is to do with the allocation of memory and i use malloc to allocate that memory but im stumped and have no idea. I have successfully allocated memory earlier on in my code.

    I am useing the C gcc in Linux 32bit.
     
  2. BlackDragon

    Mobster

    Joined: 9 Jun 2006

    Posts: 2,630

    I found a couple of links here and here
     
  3. _TubbZ_

    Gangster

    Joined: 30 Jun 2009

    Posts: 406

    I can't remember exactly how you do it but you use gdb command in linux, firstly set:

    ulimit -c unlimited

    execute your app. Then do:

    gdb executable corefile

    and then type:

    where

    and it tells you where it crashed, or near abouts. (i would google it first as it may have missed out some parameters).

    When using C its paramount to know exactly where you are pointing to and whats going on as problems with dynamic memory can be a pain to work out, theres no "array OutOfBoundsException" like you get in Java - you've gotta know its not going to go out of bounds.

    if you can do what you are attempting without Dynamic memory i would go for that. if not happy hunting!
     
  4. Pho

    Sgarrista

    Joined: 18 Oct 2002

    Posts: 9,209

    Location: Derbyshire

    When you allocate memory are you checking that the pointer isn't null before you use it?

    (never done much work with malloc so I may be talking about something else!)
     
  5. mleanz1

    Hitman

    Joined: 28 Feb 2009

    Posts: 519

    Cheers guys the problem that i have is that im reading a text file(of random size and length). Unfortunately this means that i am unable to use gdb as it can't open it so fails at the point of trying to open the file. I was more looking for general tips and tricks.

    I'd love to upload the code to this or any other forum and ask for advice but its for my Uni coursework and there dead hot on copying exact code off the web. And i'd hate for them to find this forum and think that i lifted the code from here when Ive written it all myself.

    Cheers for the help though :D
     
  6. Pho

    Sgarrista

    Joined: 18 Oct 2002

    Posts: 9,209

    Location: Derbyshire

    You could zip it up first, that way the source its-self wouldn't be readable.

    Mind you, you're not plagiarising your own work anyway :p.
     
  7. mleanz1

    Hitman

    Joined: 28 Feb 2009

    Posts: 519

    Im going to change a couple of variable names and upload the section that isnt working. That way maybe someone might be able to give me a slight pointer somwhere.

    This is the code thats not working.
    Code:
            currentline = ( char * ) malloc((size_t)msize);
    	for(i = 0; i <= linecount; i++) /* linecount = varable used to no. lines */
    	{
    	currentline = strtok_r(fstring, "\n", &save1);
    	while(currentline !=NULL)
    	{	
    		name = strtok_r(line," ",&save2);
    		qu = atoi(strtok_r(NULL," ",&save2));
    		pri = atoi(strtok_r(NULL," ",&save2));	
    	}	
    		pts[i].pname = malloc(strlen(name) +1 *sizeof(char));
    		strcpy(pts[i].pname, name);
    		pts[i].quan = qu;
    		pts[i].pp = pri;
    	}
    
    Here are the variables and structure used.
    	
    	int linecount;
    
    	typedef struct 
    	{
    		char *pname;
    		int quan;
    		int pri;
    	} information;
    
    	int qu;
    	int pri;
    	char *name;	
    	char *currentline;
    	char *save1;
    	char *save2;
    
    	information pts[linecount];
     
  8. BlackDragon

    Mobster

    Joined: 9 Jun 2006

    Posts: 2,630

    Could you add print lines or something throughout the code so you know at exactly what point it is failing?
     
  9. _TubbZ_

    Gangster

    Joined: 30 Jun 2009

    Posts: 406

    Stick in a tonne of print statements displaying values and where its at to work out where abouts it gets to before failing then.

    See i like the basic feel of C and all but i hate the lack of advanced debugging tools and friendly compilers. :(

    Plus you don't have to post all your source maybe just a snippet or an example, 1 its really annoying when someone posts like a hundred+ lines of source and says "why don't this work" and 2 its not like you're re inventing how to read from a file. I found out a few weeks after one of my big programming project hand in dates part of my code had gone around half the year after i lent it to a friend and the lecturers were none the wiser as people just changed variable names and switched stuff around. Not that copying helped in the long run.

    EDIT:

    [email protected] thats the second time i was typing something and made it too long and you posted what i was about to say!!!! :)
     
  10. BlackDragon

    Mobster

    Joined: 9 Jun 2006

    Posts: 2,630

    Hehe sorry :p
     
  11. mleanz1

    Hitman

    Joined: 28 Feb 2009

    Posts: 519

    If anyone wants any of the good expanding or explaining let me know.
     
  12. _TubbZ_

    Gangster

    Joined: 30 Jun 2009

    Posts: 406

    I don't think you can do this:

    information pts[linecount];

    as array length must be constant. About the rest, i really am not too sure.

    Does currentLine hold the whole file? As that what it looks like you're doing then you over write it with the first token. If its just the current line then its dynamically set outside the loop, so if the next line is larger then it will overflow the original length.


    I would stick some prints in and see where it goes wrong :p also add loads of comments, if its a uni project they just love that!
     
  13. mleanz1

    Hitman

    Joined: 28 Feb 2009

    Posts: 519

    Yeah as far as i know it is possible to assign the array the value of linecounts as useing print statements i can prove this works.

    The file is stored in fstring and looks as so
    process21 2 1
    process23 8 1
    etc
    Im tokenizeing each line then into three sections.

    And as for the comments thats what i usually do but my lecture this year isnt a fan and says that comments are way over used and usually used when somone doesn't understand something properly so ive been trying to avoid them.
     
    Last edited: 14 Jan 2010
  14. smyth

    Hitman

    Joined: 29 Apr 2004

    Posts: 696

    Isn't needed since currentline is set in the for loop anyway.

    That runs linecout +1 times btw.



    make sure you bracket the (strlen(name) +1) bit, it should be ok since sizeof(char) returns 1 but if you were allocating mem for an int the size would be wrong.

    Also like some people have suggested above stick some printf's in so you can narrow down where it is actually failing
     
  15. Ladforce

    Hitman

    Joined: 17 Feb 2006

    Posts: 898

    Location: Fleet

    I dont see why your while loop is there checking currentline is not null. Currentline will never be null based on whats inside that loop so it will spin.

    for this line:- name = strtok_r(line," ",&save2); where does line come from?
     
  16. dangerstat

    Soldato

    Joined: 15 Nov 2008

    Posts: 5,061

    Location: In the ether

    I'd just point out that C does have a friendly compiler and it does have an advanced debugger.


    Try and read up on gdb - it's actually not that hard to use once you get used to it
     
  17. _TubbZ_

    Gangster

    Joined: 30 Jun 2009

    Posts: 406

    I know about gdb, just i don't find it as friendly as say C#'s debugger but maybe i need to do more reading ;)