0

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).

8
  • you need to give more info. raw pointer isnt dellocated unless you do it explicitly. Commented Feb 12, 2011 at 7:56
  • 2
    @Kyle S: Can you please show us a bit more code, i.e. declaration of sort_arr and where it is initialized plus the method that gives you the item.. Would be helpfull to help ;-) Commented Feb 12, 2011 at 8:02
  • 1
    Why (*sort_arr[i]).key_a and not sort_arr[i]->key_a? Commented Feb 12, 2011 at 8:20
  • @Mark: I have added a few more details. I realized that the problem is that once I add another pointer to a struct to the array of pointers, I cannot access the struct inserted before it without seg faulting (I believe this is the consistent problem). Commented Feb 13, 2011 at 2:30
  • @Kyle: You can post as much code on here as you want. Asking the question the way you are, as a word problem, it doesn't help many visual thinkers to get to the root of the problem. Can you copy out all your code into a new project, and strip down the code to the smallest portion that will repro the problem, and post the entirety of that code? From what you've said it sounds like that example code would be as small as (part of) your constructor, your insert method, your indexing method, and a main with two inserts & one index, and won't expose any more "interesting" (proprietary) code. Commented Feb 13, 2011 at 4:23

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Make sure that this->num_items as well asi is less than this->max_items and greater than -1, as this could be the cause of seg-fault.

2 Comments

he is adding/appending an item. so it seems ok.
I believe this->num_items refers to the number of items stored in the array, not the length; he's trying to set the next available pointer
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.