Filling an array of pointers.

Started by
10 comments, last by Brocketino 17 years, 6 months ago
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
Advertisement
I think your problem lies inside the GetName function.
Is there a good reason for not using std::string & std::vector?
Can you post the code for your GetName method?
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
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
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.
sorry, that ^^^ was a really bad reply, I hope you get what I mean
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.
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

This topic is closed to new replies.

Advertisement