Thread: weird seg fault

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    weird seg fault

    Hi,

    something very weird is happening in my program.

    It works by makign some calculations and filling in a struct with the desired results to print them. Thing is, it suddendly gives me a seg fault while printing when before it didn't.


    Code:
    ...
    typedef struct _sidb{
    	int nums;
    	int layer;
    	struct _sidb *nexts;
    	} sidb;
    ...
    sidb *creates(int sidn, int layer){
    	sidb *s=malloc(sizeof(sidn));
    	s->nums=sidn;
    	s->layer=layer;
    	s->nexts=NULL;
    	return s;
    	}
    ...
    void inseres(aidb *a, int sidn, int layer){
    	sidb *s=creates(sidn, layer);
    	if (a->firsts==NULL){
    		a->firsts=s;
    	} else {
    		s->nexts=a->firsts;
    		a->firsts=s;
    		}
    	return;
    	}
    ...
    /* Calls to the functions */
    
    inseres(ptr, b->sid, b->layer);
    ...
    inseres(ptr, w->sid, w->layer);
    ...
    /* No more calls */
    ...
    for(ia=la->firsta; ia; ia=ia->nexta){
         if( ia->firsts){
            printf(" ... %d", ia->numa);
            for(is=ia->firsts; is; is=is->nexts)
                  printf(" ... %d \t %d \n", is->nums, is->layer);
            }
    ...

    This code doesn't give me problems, but his one: (EDIT: pretty much all changes to the code above are envolve 'double' and/or 'area' words)


    Code:
    ...
    typedef struct _sidb{
    	int nums;
    	int layer;
    	double area;
    	struct _sidb *nexts;
    	} sidb;
    ...
    sidb *creates(int sidn, int layer, double area){
    	sidb *s=malloc(sizeof(sidn));
    	s->nums=sidn;
    	s->layer=layer;
    	s->area=area;
    	s->nexts=NULL;
    	return s;
    	}
    ...
    void inseres(aidb *a, int sidn, int layer, double area){
    	sidb *s=creates(sidn, layer, area);
    	if (a->firsts==NULL){
    		a->firsts=s;
    	} else {
    		s->nexts=a->firsts;
    		a->firsts=s;
    		}
    	return;
    	}
    ...
    /* Calls to the functions */
    ...
    inseres(ptr, b->sid, b->layer, b->area);
    ...
    inseres(ptr, w->sid, w->layer, w->area);
    ...
    /* No more calls */
    ...
    for(ia=la->firsta; ia; ia=ia->nexta){
         if( ia->firsts){
            printf(" ... %d", ia->numa);
            for(is=ia->firsts; is; is=is->nexts)
                  printf(" ... %d \t %d \t %lf \n", is->nums, is->layer, is->area);
            }
    ....

    Gives me seg fault. I have verified the calculations, the strutcs and the insertion and it works fine, it only gives me seg fault in the print itself.

    Can someone help me out?

    Thanks in advance,
    Vermelho

    EDIT:

    print for first:
    Area 1
    4 2
    ...
    Area 12
    4 112

    print for second:
    Area 1
    4 2
    Seg fault
    Last edited by Vermelho; 05-10-2008 at 05:11 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Post a full failing example and you'll get a better response.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Code:
    sidb *s=malloc(sizeof(sidn));
    This isn't right. sidn is an int, so sizeof(sidn) will only give you the number of bytes in an integer, which is likely 2 or 4 on your system. To allocate enough space for your struct use:
    Code:
    sidb *s;
    s = malloc(sizeof *s);
    /* or... */
    s = malloc(sizeof(sidb));
    You also shouldn't use names that begin with an underscore. Instead of "struct _sidb", for example, you can use "struct sidb_", or just "struct sidb". Names that begin with an underscore are best avoided because they tend to belong to the implementation. The rules are more precise than that, but it's easiest just to not use leading underscores at all.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    2
    You're right, can't believe i didn't notice that. Thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. seg fault problems
    By nadamson6 in forum C++ Programming
    Replies: 11
    Last Post: 12-27-2005, 03:26 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM