Okay I've been spending lots of time on this and it's giving me a migraine so I go humble and ask the guru's here.
I have several structs
And I initilize as such.
Once my member functions are initilized and filled and the vertex pointers are filled and linked (which at this point I think is happening *fingers crossed* but am probably wrong given my problem below. )
I try and retrieve some data via, some function which takes the first object as a pointer and winds the polygon pointer to the end of the polygon list.
Why does it crash with a segmentation fault when it first tries to process the while statement? Am I accessing the variable incorrectly? I have tried using
but my compiler says that is not defined.
Or maybe my error is in the code for creating new objects vertex's or polygons, or perhaps in the code that free them at the end.
Any help is appreciated as I am apparently missing something in the way I am attempting access members of my structures and my brain is starting to feel a bit melty.
I have several structs
Code:
typedef struct vert
{
int x,y,z;
struct vert * next;
struct vert * prev;
} vertex_type;
typedef struct poly
{
int x,y,z;
struct poly * next;
struct poly * prev;
} polygon_type;
//and finally.
typedef struct object
{
char name=[20];
struct vert *vertex;
struct poly *polygon;
struct object * next;
struct object * prev;
} obj_type
Code:
obj_type object1={NULL};
obj_type *firstobj;
I try and retrieve some data via, some function which takes the first object as a pointer and winds the polygon pointer to the end of the polygon list.
Code:
func(obj_type *current)
{
polygon_type *tmp;
while((current->polygon->next) != NULL)
{
tmp=current->polygon;
current->polygon=tmp->next;
}
}
Code:
current->polygon.next
Or maybe my error is in the code for creating new objects vertex's or polygons, or perhaps in the code that free them at the end.
Code:
#include <stdlib.h>
#include <stdio.h>
#include "main.h"
int addobj(obj_type *last) {
obj_type *tmp=NULL;
while(last->next!=NULL)
{
tmp=last;
last=tmp->next;
}
tmp=(obj_type*) malloc(sizeof(obj_type));
tmp->prev = last;
tmp->next = NULL;
last->next = tmp;
last=tmp;
printf("Next obj node Created.%r");
return 1;
}
int freeobjlist(obj_type *current) {
obj_type *tmp;
while(current->next!=NULL)
{
tmp=current;
current=tmp->next;
}
while(current->prev!=NULL)
{
freepolygons(current);
freeverts(current);
freetextcoords(current);
tmp=current->prev;
free(current);
current=tmp;
printf("obj removed.%r");
}
return 1;
}
int addvertex(obj_type *current)
{
vertex_type *tmp;
if(current->vertex==NULL)
{
tmp=(vertex_type*)malloc(sizeof(vertex_type));
current->vertex=tmp;
current->vertex->prev=NULL;
current->vertex->next=NULL;
printf("First vertext created.%r");
return 1;
}
else
{
while(current->vertex->next!=NULL)
{
tmp=current->vertex->next;
current->vertex=tmp;
}
tmp=(vertex_type*)malloc(sizeof(vertex_type));
current->vertex->next=tmp;
tmp->prev=current->vertex;
tmp->next=NULL;
current->vertex=tmp;
printf("Additional Vertex added.");
return 1;
}
return 0;
}
int addpolygon(obj_type *current)
{
polygon_type *tmp;
if(current->polygon==NULL)
{
tmp=(polygon_type*)malloc(sizeof(polygon_type));
current->polygon=tmp;
current->polygon->prev=NULL;
current->polygon->next=NULL;
printf("First polygon created.%r");
return 1;
}
else
{
while(current->polygon->next!=NULL)
{
tmp=current->polygon->next;
current->polygon=tmp;
}
tmp=(polygon_type*)malloc(sizeof(polygon_type));
current->polygon->next=tmp;
tmp->prev=current->polygon;
tmp->next=NULL;
current->polygon=tmp;
printf("Additional Vertex added.");
return 1;
}
return 0;
}
int addtextcoord(obj_type *current)
{
textcoord_type *tmp;
if(current->textcoord==NULL)
{
tmp=(textcoord_type*)malloc(sizeof(textcoord_type));
current->textcoord=tmp;
current->textcoord->prev=NULL;
current->textcoord->next=NULL;
printf("First textcoord created.%r");
return 1;
}
else
{
while(current->textcoord->next!=NULL)
{
tmp=current->textcoord->next;
current->textcoord=tmp;
}
tmp=(textcoord_type*)malloc(sizeof(textcoord_type));
current->textcoord->next=tmp;
tmp->prev=current->textcoord;
tmp->next=NULL;
current->textcoord=tmp;
printf("Additional textcoord added.");
return 1;
}
return 0;
}
int freeverts(obj_type *current) {
vertex_type *tmp;
while(current->vertex->next!=NULL)
{
tmp=current->vertex;
current->vertex=tmp->next;
}
while(current->vertex->prev!=NULL)
{
tmp=current->vertex->prev;
free(current->vertex);
current->vertex=tmp;
printf("vertex removed.%r");
}
if(current->vertex!=NULL)
{
free(current->vertex);
current->vertex=NULL;
}
printf("Master vertex cleared.%r");
return 1;
}
int freepolygons(obj_type *current) {
polygon_type *tmp;
while((current->polygon->next) != NULL)
{
tmp=current->polygon;
current->polygon=tmp->next;
printf("polygon wound to end.");
fflush(stdout);
}
while(current->polygon->prev!=NULL)
{
printf("Polygon deletion initiated.");
fflush(stdout);
tmp=current->polygon->prev;
free(current->polygon);
current->polygon=tmp;
printf("polygon removed.%r");
}
if(current->polygon!=NULL)
{
free(current->polygon);
current->polygon=NULL;
}
printf("Master polygon cleared.%r");
return 1;
}
int freetextcoords(obj_type *current) {
textcoord_type *tmp;
while(current->textcoord->next!=NULL)
{
tmp=current->textcoord;
current->textcoord=tmp->next;
}
while(current->textcoord->prev!=NULL)
{
tmp=current->textcoord->prev;
free(current->textcoord);
current->textcoord=tmp;
printf("textcoord removed.%r");
}
if(current->textcoord!=NULL)
{
free(current->textcoord);
current->textcoord=NULL;
}
printf("Master textcoord cleared.%r");
return 1;
}
Any help is appreciated as I am apparently missing something in the way I am attempting access members of my structures and my brain is starting to feel a bit melty.
Comment