[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2001-08-01
3
Medium Priority
?
313 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

656 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