I have several structs
Expand|Select|Wrap|Line Numbers
- 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
Expand|Select|Wrap|Line Numbers
- 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.
Expand|Select|Wrap|Line Numbers
- func(obj_type *current)
- {
- polygon_type *tmp;
- while((current->polygon->next) != NULL)
- {
- tmp=current->polygon;
- current->polygon=tmp->next;
- }
- }
Expand|Select|Wrap|Line Numbers
- 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.
Expand|Select|Wrap|Line Numbers
- #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.