iDevGames Forums
Pointer-related woes (C/C++) - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Pointer-related woes (C/C++) (/thread-4601.html)

Pointer-related woes (C/C++) - sealfin - Jan 18, 2006 10:58 AM

Okay, I've the rather strange situation where an array of pointers will give different addresses for those pointers in two different methods; in a third method, it will give the same address as in the first method (the correct address.)

Between the two method calls, the array is not manipulated in any way.

Any suggestions? I'm being driven insane by this, and Xcode's debugger isn't helping (why won't it show the attributes of a class half the time? Mad)

(I'm trying to share pointers between two objects using private methods to let a child object take the pointers held by a parent object, so that the pointers remain hidden outside of those objects.)

The array is initialised as:

Type **m_myArray = (Type**)malloc( sizeof( Type*) * m_noOfPointers );

The first method assigns the pointers into the array as:

Uint8 loop = 0;
char *label;
while( !m_myBinaryTreeOrderedItemLabels->IsEndOfQueue())
    m_myArray[ loop ] = m_myBinaryTree->Bind(( label = m_myBinaryTreeOrderedItemLabels->Pop()));
    fprintf( stderr, "pointer on assignment: %p\n", ( void* ) m_myArray[ loop ] );
    free( label );
    loop ++;

The second method returns an indexed pointer from the array as:

printf( stderr, "pointer on return:  %p (%d)\n", ( void* )m_myArray[ p_myArrayItemNo ], p_myArrayItemNo );
return m_myArray[ p_myArrayItemNo ];

Pointer-related woes (C/C++) - Fenris - Jan 18, 2006 11:29 AM

Spontaneous response: does Bind() copy the contents of label? Because if not, free (label) will destroy the memory for all of your objects...

Pointer-related woes (C/C++) - sealfin - Jan 18, 2006 01:22 PM

Circumvented the strangeness by returning a pointer to the array itself, rather than returning a pointer held in the array, passing a different index to the method for the entire array Rolleyes Weird when such strangess results in you developing a more elegant solution than the original - I really don't know why I just didn't do that in the first place Rolleyes

@Fenris: Bind() just takes the label as the label of the leaf to look for in the tree; it doesn't make a copy, and shouldn't make a copy - this probably would've been clearer if I hadn't obfuscated the identifiers - maybe I shouldn't in future, but if I hadn't, I think the only reply I'd receive is "wtf?" Blush