• Advertisement
Sign in to follow this  

Filling an array of pointers.

This topic is 4177 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok I have an array of pointers to type Tptr and it is initialised like this:
Tptr** iContactCache;

// Allocate for the contact cache
TInt32 nrOfLines = NrOfLines(); // Number of entries

// pltAlloc just calls new and does some logging
iContactCache = (Tptr**)platAlloc(nrOfLines * sizeof(Tptr*));

TInt32 namlen;
	  
// Now I fill the contact cache
for ( int i=0 ; i<NrOfLines() ; i++ )
{
  // GetName retrieves a name from the contact list
  const TChr* nam = GetName(i, namlen);

  // This is then  cast to a Tptr and inserted into the list
  iContactCache = new Tptr(const_cast<TUint8*>(nam), namlen);
}

Unfortunately when I later come to inspect the contactcache, all I get in any position, is the last entry that was added. All the functions such as getname etc are working fine, so I just wondered if you fine folks could spot anything wrong with my handling of the arrays?? Any help greatly appreciated. Mark

Share this post


Link to post
Share on other sites
Advertisement
I have tested the GetName function and the correct values come out everytime.

Some how when they are inserted they get messed up because I when I extract them at the other end, every record is the same, and it is the last record that was added.

Here is the extraction code. You can ignore pretty much everything other than the Tptr* tempContact = iContactCache. I have watched the tempContact variable whilst stepping through and it is the same every time, despite being able to see that different values ARE being put in at the other end??


for ( i=0; i < NrOfLines(); i++ )
{
Tptr* tempContact = iContactCache;

if ( CollatedCompare( tempContact, iEntered ) == 0 ) // starts with...
{
iMatch[iNrMatches++]=i;
}
}



I really can't understand what could be going wrong, everything seems fine to me??

Mark

Share this post


Link to post
Share on other sites
xstreme2000: I cannot because this is running on in house software on an embedded device.

Brocketino: This method is used throughout the code with no problems whatsoever, here it is.


const TChr* CContactManager::GetName(TInt32 anIndex,TInt32& namlen)
{
TBuf8<256> tmpTxt;
// NOTE: Use GetCellTextAllContacts and GetCellTextContact (iso GetCellText) here since we're still in the
// process of constructing the iMatch vector. Which is used in GetCellText.
if( iInput->iIsContactAdresses )
tmpTxt.Copy(GetCellTextAllContacts(anIndex));
else
tmpTxt.Copy(GetCellTextContact(anIndex));

tmpTxt.ZeroTerminate();

Safecopy(iCellbuf, tmpTxt);
const TChr* p = iCellbuf.PtrZ();
TInt32 len=iCellbuf.Length();
while ( len>0 && p[len-1]!='|')
len--;
namlen = iCellbuf.Length()-len;
return p+len;
}




This listing is not really any use because it calls so many other methods, but I am positive that it works fine.

Mark

Share this post


Link to post
Share on other sites
Fair enough, I could be wrong because I'm not really sure what some of those variables are but at a guess I'd say that each call to GetName() is returning a pointer to the same block of memory which is being overwritten each time by the latest line read. So something along the line of this is happening

char buf[256]; // global var

char *GetName() {
readFromFileIntoBuffer(buf);
return buf;
}

// in main()...

char *p1 = GetName(); // p1 = &buf[0], buf = first name

char *p2 = GetName(); // p2 = &buf[0], buf = second name therefore p1 also = second name since it also points to buf.

Share this post


Link to post
Share on other sites
I was thinking that perhaps your GetName function was returning a pointer to a variable that just will be filled with new data at each call.

char *GetName(int index,int len) {
char* ptr = (char*)malloc(len);
// fill buffer with data
return ptr;
}

Sorry didnt see the above post.

Share this post


Link to post
Share on other sites
Quote:
Original post by Brocketino
I was thinking that perhaps your GetName function was returning a pointer to a variable that just will be filled with new data at each call.


Hehe, that's what I was saying, just I said it very badly :p

Share this post


Link to post
Share on other sites
Solved!

This line was being created on the stack, so each pointer in the following line ended up pointing to the same thing, which was the last stack address before the loop exited.

// GetName retrieves a name from the contact list
const TChr* nam = GetName(i, namlen);

I hate working with unfamiliar types. Thanks for the input guys.

Mark

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement