Click here to Skip to main content
16,008,175 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Populating Combo box with data from a database(^_^!!) Pin
Antti Keskinen8-Feb-04 10:23
Antti Keskinen8-Feb-04 10:23 
GeneralDynamically setting up DSN (^_^!!) Pin
solostar***7-Feb-04 19:55
solostar***7-Feb-04 19:55 
GeneralRe: Dynamically setting up DSN (^_^!!) Pin
Antti Keskinen8-Feb-04 10:18
Antti Keskinen8-Feb-04 10:18 
GeneralStructure parser ... Pin
super-kingkong7-Feb-04 19:45
super-kingkong7-Feb-04 19:45 
Generalclient to client communication Pin
rasha20037-Feb-04 18:47
rasha20037-Feb-04 18:47 
GeneralRe: client to client communication Pin
John M. Drescher7-Feb-04 19:12
John M. Drescher7-Feb-04 19:12 
GeneralRe: client to client communication Pin
rasha20037-Feb-04 19:19
rasha20037-Feb-04 19:19 
GeneralPointer templates and delete errors Pin
Leo Smith7-Feb-04 18:09
Leo Smith7-Feb-04 18:09 
I am having problems deleting a char string that I created with a new function and added to a node template to use within a double linked list (also a template). I create a string copy the contents into it than add the pointer to a new node item. Later when I pull the string back out and try to delete the string or the node, I get a debug error: "DAMAGE: after block (#53) at mem address". Here is a sample of the basic code.

void TestFunc()
{
char cIn[80];
char *pStr = NULL;
CDblList<char> pList;
bool bDone = false;

while(!bDone)
{
cout << "\n Input Text (E to exit): " << flush;
cin >> cIn;
if(stricmp(cIn, "E") == 0)
break;
pStr = new char[strlen(cIn)];
strcpy(pStr, cIn);
pList.AddNodeToTail(pStr);

if(pList.GetCount() > 0)
{
pStr = pList.RemoveHead();
cout << " Removed from head: " << pStr << endl;
delete [] pStr;
while(!bDone)
{
pStr = pList.RemoveTail();
if(pList.GetCount() + 1 > 0)
{
cout << " Removed from tail: " << pStr;
cout << "\t Count: ";
cout << pList.GetCount() + 1 << endl;
delete [] pStr;
}
else
{
pList.Flush();
bDone = true;
}
}
}
}

//////// Different header file
template <class t=""> class CDblList; // Forward declaration

template <class t=""> class CNode
{
private:
T* pVal;
CNode<t>* pNext;
CNode<t>* pPrev;
public:
CNode() : pVal(0), pNext(0), pPrev(0) {;}
~CNode(void)
{
pVal = NULL;
pNext = NULL;
pPrev = NULL;
}

friend class CDblList<t>;
};

template <class t=""> class CDblList
{
private:
CNode<t>* pNode;
int iCount;
public:
CDblList() : pNode(0)
{
iCount = 0;
pNode = new CNode<t>;
}
~CDblList() { Flush(); }
void Flush()
{
if(pNode)
{
CNode<t>* p = (CNode<t> *) pNode->pPrev;
pNode->pPrev = NULL;
while(p != pNode)
{
p = p->pPrev;
delete p->pVal;
delete p->pNext;
}
p = NULL;
}
iCount = 0;
}
void AddNodeToHead(T* pItem)
{
if(!pItem)
return;
CNode<t>* p = new CNode<t>;
p->pVal = pItem;
if(iCount == 0)
{
pNode->pNext = p;
pNode->pPrev = p;
p->pNext = pNode;
p->pPrev = pNode;
iCount ++;
}
else
{
p->pPrev = pNode;
p->pNext = pNode->pNext;
p->pNext->pPrev = p;
pNode->pNext = p;
iCount++;
}
}
T* RemoveHead()
{
CNode<t>* p = pNode->pNext;
char *pRet = NULL;
if(!p)
return NULL;
else
{
pNode->pNext = p->pNext;
p->pNext->pPrev = pNode;
pRet = p->pVal;
p->pVal = NULL;
iCount --;
if(iCount >= 0)
delete p;
}
return pRet;
}
int GetCount() { return iCount; }
};

A couple of other notes. A couple of functions have been removed to save space. They have nothing to do with adding removing information, unless it is a duplicate of a similar function but operating in the reverse. The linked list is doubly linked and circular. The app is meant to avoid windows headers and is using only <fstream.h> and <string.h>. This is a console app that I amtrying to build and run on an NT based system. I am compiling under VC 6.0. By the way, I don't get any errors saying that I didn't cleanup memory when I comment out all the delete functions.

Thanks for any assistance in helping see what I am doing wrong. I am doing this to learn and understand templates. Dead | X|

Leo T. Smith
GeneralRe: Pointer templates and delete errors Pin
Michael Dunn8-Feb-04 6:21
sitebuilderMichael Dunn8-Feb-04 6:21 
GeneralComboBox problem.... Pin
je_gonzalez7-Feb-04 17:25
je_gonzalez7-Feb-04 17:25 
GeneralRe: ComboBox problem.... Pin
je_gonzalez7-Feb-04 17:28
je_gonzalez7-Feb-04 17:28 
QuestionAre users of &quot;IP*Works!&quot; here? Pin
Uwe Keim7-Feb-04 16:38
sitebuilderUwe Keim7-Feb-04 16:38 
AnswerRe: Are users of &quot;IP*Works!&quot; here? Pin
je_gonzalez7-Feb-04 17:19
je_gonzalez7-Feb-04 17:19 
GeneralRe: Are users of &quot;IP*Works!&quot; here? Pin
Uwe Keim7-Feb-04 18:08
sitebuilderUwe Keim7-Feb-04 18:08 
AnswerRe: Are users of &quot;IP*Works!&quot; here? Pin
Joe Woodbury7-Feb-04 18:01
professionalJoe Woodbury7-Feb-04 18:01 
Generaljust a small syntax question Pin
Ylis7-Feb-04 13:37
Ylis7-Feb-04 13:37 
GeneralRe: just a small syntax question Pin
Jörgen Sigvardsson7-Feb-04 14:00
Jörgen Sigvardsson7-Feb-04 14:00 
GeneralRe: just a small syntax question Pin
Ylis7-Feb-04 14:13
Ylis7-Feb-04 14:13 
GeneralRe: just a small syntax question Pin
Jörgen Sigvardsson7-Feb-04 14:18
Jörgen Sigvardsson7-Feb-04 14:18 
GeneralMultithreading assertion error Pin
sschilachi7-Feb-04 8:44
sschilachi7-Feb-04 8:44 
GeneralRe: Multithreading assertion error Pin
Jörgen Sigvardsson7-Feb-04 14:02
Jörgen Sigvardsson7-Feb-04 14:02 
GeneralRe: Multithreading assertion error Pin
Tom Larsen8-Feb-04 7:47
Tom Larsen8-Feb-04 7:47 
QuestionHow can I figure out the reason of not working of the intellisense property of Visual Studio ? Pin
Gem7-Feb-04 7:31
Gem7-Feb-04 7:31 
AnswerRe: How can I figure out the reason of not working of the intellisense property of Visual Studio ? Pin
Jörgen Sigvardsson7-Feb-04 14:05
Jörgen Sigvardsson7-Feb-04 14:05 
GeneralRe: How can I figure out the reason of not working of the intellisense property of Visual Studio ? Pin
Gem7-Feb-04 23:14
Gem7-Feb-04 23:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.