Assigning pointer to an index in an array of pointers
<p>I have a C++ class where I have a dynamically-allocated array of pointers to structs. I have a member function to "add an item" to this array by assigning an index of the array to the pointer to a dynamically allocated instance of the struct.</p>

<p>I have <code>sort_arr</code> initialized with <code>sort_arr = new node *[this-&gt;max_items];</code>.</p>

<p>In my assignment function I have <code>sort_arr[this-&gt;num_items] = item;</code> where the pointer is being passed as an argument with <code>node *item</code>.</p>

<p>In this function, I am able to access a member variable using <code>(*sort_arr[i]).key_a</code> (where i is the index), but once another item is added, this reference is no longer valid and causes a seg fault.</p>

<p>Is the pointer being deallocated, and if so, is it possible to prevent this?</p>

<p><strong>EDIT</strong>: Sorry for the ambiguity here. I am trying to understand the problem generally and not specifically (in a pedagogical sort of way). I was hoping it was a problem with my conceptual approach. Given that it probably isn't, here are some more details:</p>

<p>node is defined as <code>node **sort_arr;</code> in the class declaration and then initialized by the constructor as <code>sort_arr = new node *[this-&gt;max_items];</code>. The insert method of the class executes: <code>sort_arr[this-&gt;num_items] = item;</code>, where <code>item</code> is passed with <code>node *item</code>.</p>

<p>It seems that after an item 'n2' is inserted after 'n1', 'n1' is no longer accessible via the reference <code>(*sort_arr[num_items]).key_a</code>. <code>key_a</code> is a member variable of the <code>node</code> struct.</p>

<p><strong>EDIT 2</strong>: <code>node *item</code> is dynamically allocated outside of the class (in the <code>main</code> function).</p>
