Solved

CObList have problem of check it's content???

Posted on 2001-08-01
3
307 Views
Last Modified: 2010-04-02
Hi:
  Here I have a block of code use CObList to store data. I check the list whether it contain the data I want to add or not before I added the input Data to the list in the following code.
.h file
void test(LPCSTR inputdata)

.cpp file
test(inputdata)
{
....
char* lpBuf;
memcpy(lpBuf, inputdata, Size);
lpBuf[lSize] = '\0';

POSITION posCount;
for(posCount = m_PdclistSend.GetHeadPosition(); posCount != NULL;)
{
(char*) lpList;
lpList = (char*)m_PdclistSend.GetNext(posCount);
if (strcmp(lpList,lpBuf)!=0)
{
                    m_PdclistSend.AddTail((CObject*)lpBuf);
}
}
}
With this block of code. It has a strange output.
The list size increse strange and some have strange symbol, I add ten inputdata first, then send ten same  message to this method again. It expected not add data to the list. But it add to the list and the list size increase from 10 to 100, 5000....

So I use another method.
{
...
POSITION pos;
if( ( pos = m_PdclistSend.Find( (CObject*)lpBuf)) == NULL ) // Hunt for lpBuf     {                          .
m_PdclistSend.AddTail((CObject*)lpBuf);
}
But the function return null always even i know the ObList has contain  the input data.So it still add inputdata to the lsit and cause problem.
Since it is an enhancement work. I cannot change the list type,

So anybody have idea of how to  check the content of the list with the inputdata without affect the content of COblist.
Millions of thanks in Advance.
                     

0
Comment
Question by:wky086
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6340153
>>m_PdclistSend.AddTail((CObject*)lpBuf);

Well, you're adding the object you found to the list again, so the increase in size isn't really surprising...
0
 

Accepted Solution

by:
sandeepsehgal earned 50 total points
ID: 6340327
Hi wky086,

Some of the major problems that appear to be in your code include :

char* lpBuf;
memcpy(lpBuf, inputdata, Size);
lpBuf[lSize] = '\0';

You are not allocating memory for lpBuf, but are copying data into it, this shall cause over runs, it should have caused your code to crash, but perhaps it is not crashing due to memory overwrite on the memory that is owned by your program. The memory overwrite is causing memory to be corrupted.

Look at the sample code listed hereunder.

The function AddItemToList adds an item to the list if it does not already exist in the list. It returns a BOOL value which specifies whether the item has been added to the list or not.

class MyList
{
private:
     CObList * m_pList;
public:
     MyList()
     {
          m_pList = new CObList;
     }
     ~MyList()
     {
          m_pList->RemoveAll();
          delete m_pList;
          m_pList = NULL;
     }
     BOOL AddItemToList(LPCTSTR lpPtr)
     {
          BOOL bFound = FALSE;
          LPSTR lpBuf = NULL;
          int nSize = strlen(lpPtr);
          lpBuf = new TCHAR[nSize+1];
          memcpy(lpBuf, lpPtr, nSize);
          lpBuf[nSize] = '\0';
          POSITION posCount = NULL;
          for(posCount = m_pList->GetHeadPosition(); posCount != NULL;)
          {
               LPSTR lpItem = (LPSTR)m_pList->GetNext(posCount);
               if (strcmp(lpItem,lpBuf) == 0)
               {
                    bFound = TRUE;
                    break;
               }
          }
          if (bFound == FALSE)
               m_pList->AddTail((CObject *)lpBuf);
          return bFound;
     }
};

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
     int nRetCode = 0;

     // initialize MFC and print and error on failure
     if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
     {
          // TODO: change error code to suit your needs
          cerr << _T("Fatal Error: MFC initialization failed") << endl;
          nRetCode = 1;
     }
     else
     {
          // TODO: code your application's behavior here.
          CString strHello;
          strHello.LoadString(IDS_HELLO);
          cout << (LPCTSTR)strHello << endl;
     }

     MyList oMyList;
     BOOL bFound = FALSE;
     for (int i = 0; i < 10; i++)
     {
          CString strItem;
          strItem.Format("%d%d%d%d%d%d", i,i,i,i,i);
          bFound = oMyList.AddItemToList(strItem.operator LPCTSTR());
          if (bFound == TRUE)
          {
               cout << "Found";
          }
     }

     for (i = 0; i < 10; i++)
     {
          CString strItem;
          strItem.Format("%d%d%d%d%d%d", i,i,i,i,i);
          bFound = oMyList.AddItemToList(strItem.operator LPCTSTR());
          if (bFound == FALSE)
          {
               cout << "Not Found";
          }
     }

     return nRetCode;
}

Try it out.

Sandeep
0
 

Author Comment

by:wky086
ID: 6342900
Thank you very much. I will try it out
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Lambda for random numbers problem 7 116
Template syntax for variable length arrays 9 75
C++ mouse_event mouse look 7 97
Why  my code (program) build with old compiler? 11 77
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question