// I didn't want to change the original post because it might confuse people. My new question is below.
I am having trouble with what I think is a memory leak with my pointers. When the code is run the list is printed as expected but then the program ends with an error "kernel.exe has encountered a problem and needs to close. We are sorry for the inconvenience." (in Windows XP)
Here is my code:
#ifndef _iostream_included_
#define _iostream_included_
#include <iostream>
#endif
#ifndef _kernel_included_
#define _kernel_included_
#include "kernel.h"
#endif
#ifndef _list_included_
#define _list_included_
#include "list.h"
#endif
#define version 0.1
usingnamespace std;
int main() {
cList slist(6);
slist.newNode(7);
slist.newNode(89); // Values to test the list with
slist.newNode(54);
slist.newNode(52);
slist.print(); // this member function of cList:: seems to be the problem
std::cin.get();
return 0;
}
class cKernel {
public:
cKernel(int input) { data = input; }
int print() { return data; }
void setData(int input) { data = input; }
void setNext(cKernel * node) { next = node; }
bool isset() { return set; }
cKernel * returnNext();
private:
bool set; // keep track if *next is set
int data; // data that the node holds
cKernel *next; // pointer to next node
};
cKernel * cKernel::returnNext() {
if(next) {
return next;
}
else {
cKernel * tmp = new cKernel(-1); // supposed to be
// a error object so I can tell *next is not set. Probably a bad way to do it //though...
return tmp;
}
}
#ifndef _iostream_included_
#define _iostream_included_
#include <iostream>
#endif
#ifndef _kernel_included_
#define _kernel_included_
#include "kernel.h"
#endif
class cList {
public:
cList(int data);
~cList();
void print();
void newNode(int data);
private:
cKernel *next;
cKernel *head;
};
cList::cList(int data) {
next = new cKernel(data);
head = new cKernel(-1);
head->setNext(next);
}
cList::~cList() {
delete head; // this function needs some work to clean up pointers
delete next; // Any suggestion for how to delete all of them?
}
// This appears to be the problem function
void cList::print() {
cKernel * tmp = head->returnNext();
int rep = 0;
while(tmp->isset() != 0)
{
rep++;
std::cout << rep << ": " << tmp->print() << std::endl;
if((tmp->returnNext())->isset())
{
tmp = tmp->returnNext(); //<--- I think the problem is here
}
}
}
void cList::newNode(int data) {
cKernel * tmp = new cKernel(data);
next->setNext(tmp);
next = tmp;
}
----------------------------------------------
I just want a FIFO structure list. I think the problem is in list.h. Maybe I am just missing something obvious. I am also not sure how to accomplish deleting all the pointers that I have created. Thanks for the help.
I figured it out, I was just stupid and line 39 of list.h I made the if statement and forgot to include the else part of it to break from the while loop.
I solved the problem above and wanted to improve the above code to work with templates. This is my first real use of templates and I apparently made a syntax error. GCC returns these two errors: "26 C:\Dev-Cpp\projects\source\kernel\kernel.h expected constructor, destructor, or type conversion before '*' token" and "26 C:\Dev-Cpp\projects\source\kernel\kernel.h expected `;' before '*' token". Here is my code: