Memory related problem in C++ program
Hello,
I was just trying to implement a simple linked list in C++ using class.
The program is not complete. I am just building it. But facing problem in one of the function called "create_node".
--------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <cstdlib>
using namespace std;
class node
{
int data;
node *link;
public:
node();
~node();
void create_node(int);
node* get_link();
int get_data();
};
node::node()
{
}
node::~node()
{
}
void node::create_node(int x)
{
this->data = x;
if(this->link == NULL)
this->link = new node;
this->link = this + 1;
}
node* node::get_link()
{
node temp;
temp.link = this->link;
return temp.link;
}
int node::get_data()
{
node temp;
temp.data = this->data;
return temp.data;
}
void main()
{
node *head, *next;
head = next = new node;
head->create_node(10);
cout << "Data 1 = " << head->get_data() << "\n";
next = head->get_link();
//next->create_node(20);
cout << "Data 2 = " << next->get_data() << "\n";
delete head;
delete next;
system("pause");
}
----------------------------------------------------------------------------------------------------
This program works fine if I do not call "create_node" function more than one time. When this function is called for the second time, I get "unhandled exception" error during execution. I am not sure if my implementation of "create_node" function is correct. But I guess this must be a memory related problem.
In create_node function, I am just trying to set the data to the value passed and increment the "link" pointer, so that it points to next node. I also allocate memory using "new", before I assign value to the "link" pointer.
Please let me know what is the problem in my "create_node" function?
Thank you in advance.
Kind regards,
Hema
Re: Memory related problem in C++ program
Your code has a few errors and is badly designed.
C++ already has a double-linked list class std::list, use it.
If this is a homework project and you have to roll your own list then you need a better design. You should make two classes, a node class and a list class. The node class holds a pointer to a node and the data item. The list class holds a pointer to a node. The node class could and should be private to list. You dont interact with nodes from client code, you interact with the list class which in turn uses the node class heavily in its implementation.
So we end up with a starting point of ....
Code:
class List
{
private:
struct Node
{
int data;
Node* next;
};
Node* head;
};
Next you need to decide what services your list class will offer to client code, what constructors it should have etc. and then fill in the missing interface and implement it. finally write a main function (which always returns int and never void) which uses the list class.
Re: Memory related problem in C++ program
Thank you so much. This is just a home work project and I am learning C++.
I would implement as you suggested. Thank you for your input.