简体   繁体   中英

Assigning pointer to an index in an array of pointers

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.

I have sort_arr initialized with sort_arr = new node *[this->max_items]; .

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

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

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

EDIT : 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:

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

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

EDIT 2 : node *item is dynamically allocated outside of the class (in the main function).

The code you posted looks basically correct (if not the best way to do this sort of thing), but I can't tell what key_a is, or what context you are calling it in. Because of that, it's hard to tell exactly what the problem is. Posting the entire body of your function might be useful.

The only way something you allocated via new will be deallocated is if you (or some code you call) explicitly calls delete . That's pretty much the whole point of dynamic memory allocation, to allow your objects to live after the stack frame gets popped off.

My best guess with the current information is that you're trying to access a local value that got allocated on the stack after returning from the function. For example, this would cause a problem:

some_type* some_function(int i)
{
    // ...
    some_type p = (*sort_arr[i]).key_a; // p is a copy of key_a, allocated on the stack
    // ...
    some_type* result = &p;
    return result;
}

In this scenario, p would be okay to return directly (if you changed the return type to some_type instead of some_type* ), but you can't return a pointer to a local value. The local value is no longer valid after the function exits. This often causes a segfault.

确保this->num_items以及i小于this->max_items且大于-1 ,因为这可能是seg-fault的原因。

Don't use dynamic arrays if it isn't for lecturing. Use a simple and save std::vector . It handles nearly everything that could go wrong. Try with that and see if there's still a seg-fault.

As noted, the code does appear to be correct. The problem was unrelated to the referenced code. I had been debugging some memory leaks and was deleting the referenced items, which was in turn causing the problem (quite obviously now that I see that).

I appreciate everyone's help and I'm sorry if I drove anyone crazy trying to find what was wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM