- Let’s create a CPtrList copy function now.
- We now create a new CPtrList variable and call the function as following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | void CopyLinkList(CPtrList *pDestList, CPtrList *pSrcList, int size) { // Always check the validity of pointer list source if (!pSrcList) return; // Copy from source to destination for (POSITION pos = pSrcList->GetHeadPosition(); pos != NULL;) { LPVOID pNode = pSrcList->GetNext(pos); if(IsBadReadPtr(pNode, 1)) break; char * pChar = (char*)malloc(size); memcpy(pChar, pNode, size); pDestList->AddTail(pChar); } } |
1 2 3 4 5 6 | // CPtrList variable - this will hold the copy data CPtrList pNewList; // We call the following function to initialize the // copy process CopyLinkList(&pNewList, &pList, sizeof(ST_NAME)); |
Copy CPtrList