For anyone who is unfamiliar as to what a Skip List is, all that matters is that it's a linked list that has an array of pointers for the next (And in my case, also previous), pointing to specific other entries in the linked list.
I am currently implementing the Node that contains the the value and the pointer references for the next relevant nodes.
When I run it in Valgrind, I get the following error:
Invalid write of size 8
==3201== at 0x109492: main (testfile.c:53)
==3201== Address 0x1ffefffde0 is on thread 1's stack
==3201== 240 bytes below stack pointer
==3201==
==3201== Invalid write of size 8
==3201== at 0x1094AD: main (testfile.c:54)
==3201== Address 0x1ffefffd90 is on thread 1's stack
==3201== 320 bytes below stack pointer
I would like to know how I am supposed to implement an array of pointers such that it does not give me memory errors, or whether I am supposed to ignore Valgrind's messages.
typedef struct s_SkipElement {
int value, level;
struct s_SkipElement** next;
struct s_SkipElement** previous;
} SkipElement;
SkipElement* skipelement_create(int level) {
SkipElement *e = malloc(2*sizeof(SkipElement)+2*sizeof(int));
e->level = level;
SkipElement *tempNext[level];
SkipElement *tempPrev[level];
//printf("tempNext:%li\n",sizeof(tempNext));
e->next = tempNext;
e->previous = tempPrev;
return e;
}
int main () {
SkipElement* e = skipelement_create(10); //Tests the memory allocation of skipelement_create;
SkipElement* t = skipelement_create(10);
//printf("t%li %li\n",sizeof(t),sizeof(*t));
//printf("e%li %li\n",sizeof(e),sizeof(*t));
for (int i=0; i<e->level;i++) {
//printf("run\n");
e->next[i] = t;
e->previous[i] = t;
//printf("run2\n");
}
e->value=5;
//printf("%i %i\n",e->value,e->level);
//printf("%li\n",sizeof(e->next[4]));
(void)e;
return 0;
}
