I am implementing a skiplist for my self and I'm experiencing some problems with C++. I have two structures:
A node of the skiplist - it holds its int value, and a pointer to an array of pointers to other nodes.
struct node{ int val; node** next; };Skiplist which holds pointers to the head and tail of the list (sentinels).
struct skiplist{ node *head, *tail; };
Also, I have a function which returns a pointer to the skiplist structure (I use this function to initialize the skiplist):
skiplist* createSkipList(){ skiplist* l = new skiplist; node* listHead = new node; node* listTail = new node; node* headNext[MAX_LEVEL]; //array of pointers listHead->next = headNext; for(int i=0; i<MAX_LEVEL; i++){ listHead->next[i] = listTail; } l->head=listHead; l->tail=listTail; } And in the main() function I call:
skiplist* skiplist=createSkipList(); Everything works fine in the createSkipList() function, but if I want to refer to the skiplist in the main() i.e. by accessing skiplist->tail the program crashes. I've been searching for related posts, but they didn't help me.
As mentioned in a similar post I should't experience dangling pointers because I am using the new operator to allocate the structures. I would be grateful for any hints ;)
return l;fromcreateSkiplist().