I wanted to create some sort of linked list pointing to the array position rather than using pointers, since i was going for only allocating memory statically.
Code:
int object_index()
{
int index;
// Gets a unique index number
// First it looks for a recycled number
if (obj_recycle_size > 0) {
obj_recycle_size--;
index = obj_recycled[obj_recycle_size];
}
// Otherwise it generates a new one
else {
index = objIndex_size++;
}
// Links the new index into the chain
// Add first
if (objHead == UNDEFINED) {
objHead = index;
objHead = index;
printf("has no head!\n");
if (objTail == UNDEFINED) {
printf("has no tail!\n");
objTail = index;
}
object[index].prev = UNDEFINED; // define previous node
}
// Add after
else {
object[objTail].next = index;
object[index].prev = objTail; // define previous node
objTail = index;
}
object[index].next = UNDEFINED; // define next node
int i = objHead;
// Debug -iterates through the current list after each added object
while (i != UNDEFINED) {
int prev = object[i].prev;
int next = object[i].next;
printf("%d->%d->%d || %d %d\n", prev, i, next, objHead, objTail);
i = object[i].next;
if (i == UNDEFINED)
printf("-----\n");
}
return index;
}
However if i create 3 objects the output is the following:
as no head!
has no tail!
9999->0->9999 || 0 0 ----------- so far so good.
9999->0->1 || 0 1
0->1->9999 || 0 1 ----------- this looks proper too
9999->0->1 || 0 2
9999->1->2 || 0 2 ----------- I can't understand why it would lose it's previous value here......
1->2->9999 || 0 2