3

I have declared a basic structure as below.

struct Item
{
    MPoint key; //4 element double array x,y,z,w represents a point in space
    Item * next = NULL;
};

I have a small array of pointers to these structures

Item * arr[3];

When an item is created, the key is defined by its location which is a unique point in 3D space.

Item hti; //create a new item struct called hti
hti.key = transf.rotatePivot(MSpace::kWorld); 
Item * p_hti = &hti; //pointer to the struct
arr[0] = p_hti;

The main problem is that when i watch the arr[0] variable in my debugger, it shows the correct key values. However, as soon as I examine the data as in

double x = arr[0]->key.x;

Instead of getting the correct value for x, i get x = -9.2559631349317831e+61 every time and for all the other values in the key (x,y,z).

I assume that the strange value above represents memory that is uninitialized but it just doesn't make sense to me how the array correctly holds the value up until I try to pull the value back.

Any help would be appreciated!

DjAndyC
  • 91
  • 7
  • So is this a problem with the debugger? Or is it a problem with your program? If it's a problem with the debugger, you need to state what compiler/debugger suite you're using. – PaulMcKenzie Jun 27 '15 at 01:59
  • just a guess but the object `arr[0]` probably went out of scope between the time you do the assignment and the time you examine its contents. – Captain Obvlious Jun 27 '15 at 02:02

1 Answers1

4

In your example where you write:

 Item hti;  // declared on the stack
 // ...
 Item* p_hti = &hti; // points to item on the stack
 arr[0] = p_hti;  // points to item on the stack

You are causing this array to reference items that are in the current stack frame and which will be undefined after leaving this stack frame (or which could be corrupted if you perform an operation that corrupts the current stack). Is your dereference of this array happening in the same function? Or does it happen after you return "arr" from the function in which you initialized it? If the latter, that would explain your problem... the memory it references has gone out of scope. To prevent that issue, you should use dynamic memory allocation (with new) in initializing your array (you'll also need to remember to deallocate that after you are done with it with a corresponding delete).

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
  • The array is declared globally and the memory is dynamically allocated in my main method as `arr = new Item*[3];` When I watch the variable in the Visual Studio debugger, it remains correct once the program moves on to different functions but when I later access it as in `double x = arr[0]->key.x;` it changes to that weird value. – DjAndyC Jun 27 '15 at 02:34
  • ***arr[0] = p_hti;*** This does not to a deep copy. It sets the pointer arr[0] to equal the pointer p_hti. p_hti points to a local variable that no longer exists when hti goes out of scope. – drescherjm Jun 27 '15 at 03:38
  • ***arr = new Item*[3];*** You created a dynamic array of 3 pointers. This part is fine. However as I mentioned in my above comment the pointers point to items that are local variables to some function. When that function ends you are no longer able to derefrence these pointers because the local variables are no longer in existence. – drescherjm Jun 27 '15 at 03:46
  • Ok I understand that, but then how am I supposed to store pointers in an array or linked list without them being lost at the end of the function call that makes the structure in the first place? – DjAndyC Jun 27 '15 at 04:37
  • 1
    In this case `new`, but take a look at [std::make_unique](http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique). An array of pointers that knows how to self-destruct when you're done with them is very cool and extremely helpful. – user4581301 Jun 27 '15 at 05:01