469,927 Members | 1,329 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

memory delete problem

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;
}
//------------------------------------------
-------------------------------------------------

Nov 15 '05 #1
3 1564
MJ wrote:
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.
Nov 15 '05 #2
On Tue, 28 Jun 2005 03:57:35 -0700, MJ wrote:
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 <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

Nov 15 '05 #3
MJ
Hi
Lawrence is correct.. I got my mistake and now its working fine
Thanks a lot

Regards
Mayur

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Greg Baker | last post: by
2 posts views Thread by frustrated | last post: by
32 posts views Thread by John | last post: by
17 posts views Thread by ~Gee | last post: by
3 posts views Thread by John Black | last post: by
14 posts views Thread by placid | last post: by
27 posts views Thread by George2 | last post: by
6 posts views Thread by ashjas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.