Filling an array of pointers.

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

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 on other sites
I think your problem lies inside the GetName function.

Share on other sites
Is there a good reason for not using std::string & std::vector?

Share on other sites
Can you post the code for your GetName method?

Share on other sites
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 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 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() {
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 on other sites
sorry, that ^^^ was a really bad reply, I hope you get what I mean

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 on other sites
Quote:
 Original post by BrocketinoI 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

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 14
• 14
• 9
• Forum Statistics

• Total Topics
633368
• Total Posts
3011536
• Who's Online (See full list)

There are no registered users currently online

×