C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 18th, 2004, 12:20 PM
nevillemehta nevillemehta is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: India
Posts: 64 nevillemehta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 44 m 17 sec
Reputation Power: 7
Linked List Error....

I've tried making a simple implementation of a linked list in C. I havent posted the whole code here, only the part relevant to my query.

Here is the routine to add a node in the beginning of a list.

struct Node *head,*tail;

//addInFront
void addInFront(char *name)
{
//1. Making a pointer and allocating memory.
//********************************
struct Node *newNode;
newNode = (Node *)malloc(sizeof(Node));
newNode->name = name;
newNode->next = NULL;
//********************************

//OR

//2. Making an instance of the struct.
//********************************
struct Node newNode;
newNode.name = name;
newNode.next = NULL;
//********************************

if ( isEmpty() )
{
head = newNode;
tail = newNode;
}
else
{
newNode->next = head;
head = newNode;
}
}

Now when i use 1. (i.e. Making a pointer and allocating memory) code seems to work fine ahead. However, when i use 2. (i.e. Making an instance of the struct) there seems to be a memory alocation error (or some runtime error) when i try to access the tail pointer in any other method.

Personally i dont see any difference between 1 and 2. In the first case we're creating a pointer and then allocating memory whereas in the second case we're simple creating an instance of the struct so memory is already allocated.

Are they both doing the same thing?
Does anyone have any idea why the program crashes if i use no. 2 and try to access the tail pointer after adding the node?

Thanks
Neville

(PS: I can post the entire code if someone needs to have a look. Also. i have NO problems with the logic of adding a node. All that is working fine.)

Reply With Quote
  #2  
Old October 18th, 2004, 07:12 PM
The Phantom The Phantom is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: In the Dark City
Posts: 46 The Phantom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to The Phantom
entire code please

Reply With Quote
  #3  
Old October 19th, 2004, 05:59 AM
nevillemehta nevillemehta is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: India
Posts: 64 nevillemehta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 44 m 17 sec
Reputation Power: 7
Linked List Code....

Hello,

Please have a look at code 1 and code 2. Code 1 gives an error. Code 2 works fine. Could someone explain me why? Note, the 'major' difference lies in the addInFront().

// Code 1
// **********************
// This code gives a run time error
// when we call the printHeadTail()
// **********************

#include <stdio.h>
#include <malloc.h>

struct Node
{
char *name;
struct Node *next;
};

struct Node *head;
struct Node *tail;

int isEmpty()
{
if ( head == NULL )
return 1;
else
return 0;
}

//addInFront
void addInFront(char *name)
{
struct Node newNode;
newNode.name= name;
newNode.next = NULL;

if ( isEmpty() )
{
head = &newNode;
tail = &newNode;
}
else
{
newNode.next = head;
head = &newNode;
}
}

void printHeadTail()
{
printf("Head: %s, Tail: %s\n",head->name,tail->name);
}

void main()
{
addInFront("Neville");
addInFront("Shahlbh");
addInFront("Swapnil");
printHeadTail();
}


// Code 2
// **********************
// This code runs just fine
// The difference is in the addInFront()
// **********************

#include <stdio.h>
#include <malloc.h>

struct Node
{
char *name;
struct Node *next;
};

struct Node *head;
struct Node *tail;

int isEmpty()
{
if ( head == NULL )
return 1;
else
return 0;
}

//addInFront
void addInFront(char *name)
{
struct Node *newNode;
newNode = (Node *)malloc(sizeof(Node));
newNode->name = name;
newNode->next = NULL;

if ( isEmpty() )
{
head = newNode;
tail = newNode;
}
else
{
newNode->next = head;
head = newNode;
}
}

void printHeadTail()
{
printf("Head: %s, Tail: %s\n",head->name,tail->name);
}

void main()
{
addInFront("Neville");
addInFront("Shahlbh");
addInFront("Swapnil");
printHeadTail();
}

Thanks
Neville

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Linked List Error....


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT