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