Thread: weird seg fault

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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