473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1670
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online...
2
by: frustrated | last post by:
Before I begin, I must first make the following disclaimer: Although I have considerable programming experience, I do not consider myself by any means to be an expert C++ programmer. The following...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
17
by: ~Gee | last post by:
Hi Folks! Please see the program below: 1 #include<iostream> 2 #include<list> 3 #include <unistd.h> 4 using namespace std; 5 int main() 6 { 7 {
3
by: John Black | last post by:
Hi, In debugging a core dump, I run purify against my code, it says several "Free Memory Read" errors. All the errors are on one delete statement. Usually this means some pointer is deleted by...
14
by: placid | last post by:
Hi all, i have this struct for a binary tree node typedef struct treenode { char *word; struct treenode *right; struct treenode *left; }TreeNode;
27
by: George2 | last post by:
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do...
6
by: arashmath | last post by:
Hi everyone, I have a problem with large dynamic memory allocating. this is my code (in briefness): #include <stdio.h> #include <mem.h> #include <assert.h> #define SML_BOUNDS_CHECK
6
by: ashjas | last post by:
Hello all.. I am experiencing memory leakes in on of my applications that i am developing. the % of memory used for the application is increasing as shown in the top command on linux. The...
5
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.