Pointer-related woes (C/C++)

Moderator
Posts: 697
Joined: 2002.04
Post: #1
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:

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

The first method assigns the pointers into the array as:

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

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

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #2
Spontaneous response: does Bind() copy the contents of label? Because if not, free (label) will destroy the memory for all of your objects...
Quote this message in a reply
Moderator
Posts: 697
Joined: 2002.04
Post: #3
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

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  memory pointer tricks? Toontingy 2 4,094 Mar 31, 2009 02:35 AM
Last Post: Ingemar
  pointer cleanup questions kendric 7 4,423 Mar 29, 2009 07:48 PM
Last Post: kendric
  pointer or not? kensuguro 17 7,719 Aug 15, 2007 04:12 PM
Last Post: AnotherJake
  My AnimationManager didn't like being a pointer. milkfilk 6 3,813 Mar 24, 2007 10:03 PM
Last Post: unknown
  Hide Mouse Pointer JonTrainer 9 7,145 Nov 10, 2005 10:46 AM
Last Post: Jordan