memory delete problem

M

MJ

Hi
I have created a prog for link list
I am addig a node, displaying the LL contents, reversing the LL and
deleting the LL
In the delete() if I use free I am getting memory exception
I have used malloc in the add()at the same time

If I used new and delete its working fine

Can any one tell me the reason

I have given the prog below for your referecne

Mayur


-------------------------------------------------
//This is the program to create a linkedlist
// and reverse the linkedlist
#include <iostream.h>
#include <malloc.h>
//------------------------------------------
typedef struct node
{
int value;
struct node *next;
}mynode;
//------------------------------------------
//Global Variables
mynode *head,*tail,*temp, *head1;
//------------------------------------------
//Display the contents of the linked list
Display()
{
mynode *temp1;
temp1 = head;
while (temp1) {
cout << temp1->value << "\n";
temp1 = temp1->next;
}
return 0;
}
//------------------------------------------
//Append a node in the linked list
void Add(int value)
{
//temp = (mynode *) malloc(sizeof(mynode*));
temp = new mynode;
//temp = (mynode *) malloc(sizeof(mynode*));
temp->value = value;
temp->next = (mynode *)0;
if(head == (mynode *)0) {
head = temp;
}
else
{
mynode *temp1;
temp1 =head;
while(temp1->next != (mynode*)0){
temp1 = temp1->next;
}
temp1->next = temp;
}
}
//------------------------------------------
//Reverse the linked list
mynode * reverse(mynode *ptr)
{
mynode *temp1;
if(!(ptr && ptr->next))
{
head1 = ptr;
return ptr;
}
temp1 = reverse(ptr->next);
temp1->next = ptr;
if(head == ptr)
{
head->next = (mynode*)0;
head = head1;
}
return ptr;
}
//------------------------------------------
//Delete the linked list
void Delete()
{
while(head)
{
mynode *temp;
temp = head;
head = head->next;
//free(temp);
delete temp;

}
}
//------------------------------------------
//Main Program
int main(void)
{
for (int i =1 ; i <= 20; i++) {
Add(i);
}
Display();
reverse(head);
cout << "--------------------\n";
Display();
Delete();
return 0;
}
//------------------------------------------
-------------------------------------------------
 
A

akarl

MJ said:
Hi
I have created a prog for link list
I am addig a node, displaying the LL contents, reversing the LL and
deleting the LL
In the delete() if I use free I am getting memory exception
I have used malloc in the add()at the same time

If I used new and delete its working fine

Can any one tell me the reason

I have given the prog below for your referecne

Mayur


-------------------------------------------------
//This is the program to create a linkedlist
// and reverse the linkedlist
#include <iostream.h>
#include <malloc.h>
//------------------------------------------
typedef struct node
{
int value;
struct node *next;
}mynode;
//------------------------------------------
//Global Variables
mynode *head,*tail,*temp, *head1;
//------------------------------------------
//Display the contents of the linked list
Display()
{
mynode *temp1;
temp1 = head;
while (temp1) {
cout << temp1->value << "\n";
temp1 = temp1->next;
}
return 0;
}
//------------------------------------------
//Append a node in the linked list
void Add(int value)
{
//temp = (mynode *) malloc(sizeof(mynode*));
temp = new mynode;
//temp = (mynode *) malloc(sizeof(mynode*));
temp->value = value;
temp->next = (mynode *)0;
if(head == (mynode *)0) {
head = temp;
}
else
{
mynode *temp1;
temp1 =head;
while(temp1->next != (mynode*)0){
temp1 = temp1->next;
}
temp1->next = temp;
}
}
//------------------------------------------
//Reverse the linked list
mynode * reverse(mynode *ptr)
{
mynode *temp1;
if(!(ptr && ptr->next))
{
head1 = ptr;
return ptr;
}
temp1 = reverse(ptr->next);
temp1->next = ptr;
if(head == ptr)
{
head->next = (mynode*)0;
head = head1;
}
return ptr;
}
//------------------------------------------
//Delete the linked list
void Delete()
{
while(head)
{
mynode *temp;
temp = head;
head = head->next;
//free(temp);
delete temp;

}
}
//------------------------------------------
//Main Program
int main(void)
{
for (int i =1 ; i <= 20; i++) {
Add(i);
}
Display();
reverse(head);
cout << "--------------------\n";
Display();
Delete();
return 0;
}
//------------------------------------------

Try comp.lang.c++. This group deals with C only.
 
L

Lawrence Kirby

Hi
I have created a prog for link list
I am addig a node, displaying the LL contents, reversing the LL and
deleting the LL
In the delete() if I use free I am getting memory exception
I have used malloc in the add()at the same time

If I used new and delete its working fine

Can any one tell me the reason

I have given the prog below for your referecne

Mayur

#include <stdio.h>

is more appropriater to C.
#include <malloc.h>

Neither C nor C++ define such a header. malloc() and friends are declared
in the standard header <stdlib.h>

//------------------------------------------ typedef struct node {
int value;
struct node *next;
}mynode;
//------------------------------------------ //Global Variables mynode
*head,*tail,*temp, *head1;
//------------------------------------------ //Display the contents of
the linked list Display()
{
mynode *temp1;
temp1 = head;
while (temp1) {
cout << temp1->value << "\n";
temp1 = temp1->next;
}
return 0;
}
//------------------------------------------ //Append a node in the
linked list
void Add(int value)
{
//temp = (mynode *) malloc(sizeof(mynode*));
temp = new mynode;
//temp = (mynode *) malloc(sizeof(mynode*));

You need to allocate enough space for a structure but you only allocate
space for a pointer to a structure. Make that

temp = malloc(sizeof(mynode));

or a more maintainable form is

temp = malloc(sizeof *temp);

If you're using a C++ compiler you need the cast, but if you're writing
C++ you should be posting to comp.lang.c++.

Lawrence
 
M

MJ

Hi
Lawrence is correct.. I got my mistake and now its working fine
Thanks a lot

Regards
Mayur
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top