Array of Linked Lists C++

arrayOfPointers = new node*[arraySize];

That returns a bunch of unallocated pointers. Your top level array is fine, but its elements are still uninitialized pointers, so when you do this:

->next

You invoke undefined behavior. You’re dereferencing an uninitialized pointer.

You allocated the array properly, now you need to allocate each pointer, i.e.,

for(int i = 0; i < arraySize; ++i) {
    arrayOfPointers[i] = new node;
}

As an aside, I realize that you’re learning, but you should realize that you’re essentially writing C here. In C++ you have a myriad of wonderful data structures that will handle memory allocation (and, more importantly, deallocation) for you.

Leave a Comment