?
Solved

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

Posted on 2001-08-01
3
Medium Priority
?
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

770 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