473,386 Members | 1,720 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,386 software developers and data experts.

Linked list, New try (was:Linked list, no out put,help)

Dear group,

Thanks for all those who helped previously.

Now I get some value printed. Is that the value of the list getting
printed? Am I
really polluted the list?

Previous thread link (sorry for google)
http://groups.google.com/group/comp....8ac16a2ed4ed35

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

struct node *add_node(struct node *p, int funct_data)
{
struct node *t = p;
/* passed by values. So I save the argument value in a
* temp variable.Am I correct?*/

t = malloc(sizeof *t);
if(!t)
{
printf("mem error");
exit(EXIT_FAILURE);
}
t->data = funct_data;
return t;
}

int main(void)
{
struct node *p,*q;
int i;
q=malloc(sizeof *q);
if(!q)
exit(EXIT_FAILURE);
for(i=0 ; i< 6 ; i++)
{
q = add_node(p, i);
printf("%d\n",q->data);
}
return 0;
}

Jul 1 '06 #1
14 1832
fool schrieb:
Dear group,

Thanks for all those who helped previously.

Now I get some value printed. Is that the value of the list getting
printed? Am I
really polluted the list?
Sorry, I do not understand the question.
You do not really have a list but a set of allocated list nodes.
The value of the list cannot be printed.

Previous thread link (sorry for google)
http://groups.google.com/group/comp....8ac16a2ed4ed35
Sum the results up, please.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

struct node *add_node(struct node *p, int funct_data)
{
struct node *t = p;
/* passed by values. So I save the argument value in a
* temp variable.Am I correct?*/
Yes. Note that the argument value does not need to be saved.

t = malloc(sizeof *t);
Now you overwrite the "temp variable" to which you saved p.
if(!t)
{
printf("mem error");
Consider writing error messages to stderr instead of stdout.
exit(EXIT_FAILURE);
}
t->data = funct_data;
You forgot to link t and p, e.g.
t->next = p;
return t;
}

int main(void)
{
struct node *p,*q;
int i;
q=malloc(sizeof *q);
You assign a value to q.
p is not initialised.
if(!q)
exit(EXIT_FAILURE);
for(i=0 ; i< 6 ; i++)
{
q = add_node(p, i);
You overwrite the value of q and use p uninitialised.
This means you have a memory leak and "all bets are off",
respectively.
printf("%d\n",q->data);
}
return 0;
}


Consider this corrected version (not largely tested):

#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};

struct node *prepend_node (struct node *pSuccessor, int new_data)
{
struct node *pNew = malloc(sizeof *pNew);
if(NULL == pNew)
{
fprintf(stderr, "mem error\n");
exit(EXIT_FAILURE);
}
pNew->data = new_data;
pNew->next = pSuccessor;

return pNew;
}

void print_list (const struct node *pFirst)
{
const struct node *pNode;

printf("Contents:");
for (pNode = pFirst; NULL != pNode; pNode = pNode->next) {
printf("\t%d", pNode->data);
}
printf("\n");
}

void free_list (struct node *pFirst)
{
struct node *pNode, *pNext;

if (pFirst) {
for (pNode = pFirst; NULL != pNode; pNode = pNext) {
pNext = pNode->next;
free(pNode);
}
}
}

int main (void)
{
struct node *list = NULL;
int i;

for(i=0 ; i< 6 ; i++)
{
list = prepend_node(list, i);
print_list(list);
}
free_list(list);

return 0;
}
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jul 1 '06 #2
fool wrote:
Dear group,

Thanks for all those who helped previously.

Now I get some value printed. Is that the value of the list getting
printed? Am I
really polluted the list?

Previous thread link (sorry for google)
http://groups.google.com/group/comp....8ac16a2ed4ed35

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

struct node *add_node(struct node *p, int funct_data)
{
struct node *t = p;
Why this assignment? p just contains an indeterminate value. This will
lead to undefined behaviour.
/* passed by values. So I save the argument value in a
* temp variable.Am I correct?*/

t = malloc(sizeof *t);
if(!t)
{
printf("mem error");
Add a newline or call fflush(stdout). Otherwise the above statement is
not garenteed to appear on the console.
exit(EXIT_FAILURE);
You've allocated memory in main(). If you simply exit(), that memory
might be lost to the system. You should return to main() indicating an
error, free() the memory and then exit().
}
t->data = funct_data;
return t;
}

int main(void)
{
struct node *p,*q;
int i;
q=malloc(sizeof *q);
add_node() allocates an instance of struct node and returns. Here
you've set q to point to another instance of struct node. Then below
you overwrite q to contain the address of the node allocated in
add_node(), thus losing access to the original node and causing a
memory leak.

Either allocate the structure in main() or in add_node(), not in both.
if(!q)
exit(EXIT_FAILURE);
Again you're exiting with free()'ing the memory. This won't matter for
demos, but unless you get into the habit of free()'ing now, you'll
mess-up later on in serious code.
for(i=0 ; i< 6 ; i++)
{
q = add_node(p, i);
Here's where you overwrite the node allocated above with the one
allocated in add_node(). Remove the allocation in main() and simply let
add_node() do it.

Incidentally, since add_node() allocates a fresh copy of struct node,
each time it's called you'll have to save a copy of the pointer it
returns each time. Otherwise you simply lose access to the previously
malloc()'ed structures, again a memory leak.
printf("%d\n",q->data);
}
return 0;
}


I suggest a complete rewrite, based on either pete's code posted
earlier or sample implementations given in a good book like K&R2. You
need to understand memory allocation and deacllocation better. You'll
also need to think about the overall structure of your linked list
program.

As written, the code above is fragile and erroneous to the extreme.

Jul 1 '06 #3
"santosh" <sa*********@gmail.com> writes:
fool wrote:
Dear group,

Thanks for all those who helped previously.

Now I get some value printed. Is that the value of the list getting
printed? Am I
really polluted the list?

Previous thread link (sorry for google)
http://groups.google.com/group/comp....8ac16a2ed4ed35

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

struct node *add_node(struct node *p, int funct_data)
{
struct node *t = p;
Why this assignment? p just contains an indeterminate value. This will
lead to undefined behaviour.


to the op : see the call from main(). You didnt set p.
/* passed by values. So I save the argument value in a
* temp variable.Am I correct?*/
yes, but you havent passed anything into p.

t = malloc(sizeof *t);
if(!t)
{
printf("mem error");
Add a newline or call fflush(stdout). Otherwise the above statement is
not garenteed to appear on the console.
exit(EXIT_FAILURE);


You've allocated memory in main(). If you simply exit(), that memory
might be lost to the system. You should return to main() indicating an
error, free() the memory and then exit().


Out of curiosity and OT , what systems/OSs dont replace mallocs in the
event of a program crash or exit() call?
}
t->data = funct_data;
return t;
}

int main(void)
{
struct node *p,*q;
int i;
q=malloc(sizeof *q);


add_node() allocates an instance of struct node and returns. Here
you've set q to point to another instance of struct node. Then below
you overwrite q to contain the address of the node allocated in
add_node(), thus losing access to the original node and causing a
memory leak.

Either allocate the structure in main() or in add_node(), not in both.
if(!q)
exit(EXIT_FAILURE);


Again you're exiting with free()'ing the memory. This won't matter for


No he's not. The memory wasnt allocated. In this case..

To the OP : dont forget your "next" pointer. Look at the parameters you
are calling add_node with from main. Use a debugger or printfs to
examine the data being passed to your add_node function - you will see
"p" is not initialised and "next" is not used to keep the list.

best of luck!

Jul 1 '06 #4
Richard G. Riley wrote:
"santosh" <sa*********@gmail.com> writes:

.... snip ...
You've allocated memory in main(). If you simply exit(), that memory
might be lost to the system. You should return to main() indicating an
error, free() the memory and then exit().


Out of curiosity and OT , what systems/OSs dont replace mallocs in the
event of a program crash or exit() call?


Well, Windows versions before and inclusive of 98 sometimes failed to
recover all the memory after a process exited. I agree that today's
protected mode OSes recover all the memory, but forgetting to free()
still entails a memory leak during program execution. This could become
significant if the program in question is a daemon/service.

Besides, free()'ing allocated memory is good practise. It forces you to
mentally keep track of your resources, and be aware about the state of
your process.

Jul 1 '06 #5
Richard G. Riley wrote:
<snip>
>
Out of curiosity and OT , what systems/OSs dont replace mallocs in the
event of a program crash or exit() call?
RTOS's like vxWorks don't. Inside kernel or kernel-like environments
(task/tasklet/thread) you're not likely to get much help either.
Mark F. Haigh
mf*****@sbcglobal.net

Jul 2 '06 #6
"Mark F. Haigh" <mf*****@sbcglobal.netwrites:
Richard G. Riley wrote:
>Out of curiosity and OT , what systems/OSs dont replace mallocs in the
event of a program crash or exit() call?
RTOS's like vxWorks don't. Inside kernel or kernel-like environments
(task/tasklet/thread) you're not likely to get much help either.
Those environments are usually freestanding, not hosted, as I
understand it. Freestanding environments don't necessarily have
a malloc function in their libraries at all.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming
Jul 2 '06 #7
Ben Pfaff wrote:
"Mark F. Haigh" <mf*****@sbcglobal.netwrites:
Richard G. Riley wrote:
Out of curiosity and OT , what systems/OSs dont replace mallocs in the
event of a program crash or exit() call?
RTOS's like vxWorks don't. Inside kernel or kernel-like environments
(task/tasklet/thread) you're not likely to get much help either.

Those environments are usually freestanding, not hosted, as I
understand it. Freestanding environments don't necessarily have
a malloc function in their libraries at all.
To tell you the truth, I'm not sure if vxWorks is technically hosted or
freestanding. It's at least hosted-ish. It contains a complete C89
implementation/library. The thing is, _you_ have to write the code
that spawns a new task (ie thread), then calls the program's main()
with the appropriate arguments. Note that only one program may have a
main(), or you will get symbol collisions, since all programs loaded
share a C namespace as well as (typically) an identical view of and
identical permissions to memory and hardware resources.
Mark F. Haigh
mf*****@sbcglobal.net

Jul 2 '06 #8
fool (in 11*********************@m73g2000cwd.googlegroups.c om) said:

| Dear group,
|
| Thanks for all those who helped previously.
|
| Now I get some value printed. Is that the value of the list getting
| printed? Am I
| really polluted the list?

You still have problems (not the least of which is that your thread
has been hijacked.)

I've reworked your code into something that I like better - and I've
added the printf() statements that allow seeing what the list really
looks like.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Jul 2 '06 #9
It'd probably have been more help if I'd actually pasted the code into
the message. :*)

#include <stdio.h>
#include <stdlib.h>

/* Define data structures */

struct node
{ int data;
struct node *next;
};
struct list
{ struct node *head;
struct node *tail;
};

/* Add a node to the specified list */

struct node *add_node(struct list *q, int funct_data)
{ struct node *t = malloc(sizeof *t);

if (!t)
{ printf("add_node: memory allocation failed\n");
exit(EXIT_FAILURE);
}
t->data = funct_data;
t->next = NULL;
if (q->head) q->tail->next = t;
else q->head = t;
q->tail = t;
return t;
}

/* Free allocated nodes in list */

void zap(struct node *e)
{ if (e->next) zap(e->next);
free(e);
}

/* Test program with debug printf()s */

int main(void)
{ struct list q = { NULL,NULL };
struct node *p;
int i;

printf("\nAdding nodes to list...\n\n");
for(i=0 ; i< 6 ; i++)
{ p = add_node(&q, i);
printf("%d\n",p->data);
}

printf("\nList @ %p {%p %p}\n\n",&q,q.head,q.tail);
for (p=q.head; p; p=p->next)
{ printf("Node @ %p {%d %p}\n",p,p->data,p->next);
}
if (q.head) zap(q.head);
q.head = NULL;
return 0;
}

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Jul 2 '06 #10
Morris Dovey said:

<snip>
>
printf("\nList @ %p {%p %p}\n\n",&q,q.head,q.tail);
for (p=q.head; p; p=p->next)
{ printf("Node @ %p {%d %p}\n",p,p->data,p->next);
When printing a pointer value, you need to cast it to void * if it is not
already of that type:

printf("\nList @ %p {%p %p}\n\n",
(void *)&q, (void *)q.head, (void *)q.tail);

for (p=q.head; p; p=p->next)
{ printf("Node @ %p {%d %p}\n",
(void *)p, (void *)p->data, (void *)p->next);

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 2 '06 #11
Richard Heathfield (in p5******************************@bt.com) said:

| When printing a pointer value, you need to cast it to void * if it
| is not already of that type:

Thanks. (The scary part is that the cast actually makes sense to me.)

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Jul 2 '06 #12
In article <11*********************@m79g2000cwm.googlegroups. com>
Mark F. Haigh <mf*****@sbcglobal.netwrote:
>To tell you the truth, I'm not sure if vxWorks is technically hosted or
freestanding.
These days, both: an "RTP" is effectively hosted, while "kernel" code
is freestanding.
>It's at least hosted-ish. It contains a complete C89
implementation/library.
Complete (mostly? -- it uses the Dinkumware code) C99 library, for
RTPs. The compilers are still not quite there yet though. The
kernel-side code is significantly smaller.
>The thing is, _you_ have to write the code
that spawns a new task (ie thread), then calls the program's main()
with the appropriate arguments. Note that only one program may have a
main(), or you will get symbol collisions, since all programs loaded
share a C namespace as well as (typically) an identical view of and
identical permissions to memory and hardware resources.
Except in RTPs. RTPs do share (the read-only code of) libraries,
but have private data space and namespaces.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jul 2 '06 #13
The code I posted showed assembly of a FIFO list. As usual, I had
second thoughts and decided that the OP may have been trying to build
a LIFO list. Since sleep wasn't much of an option, I put the following
together before wandering off...

[ With thanks to RJH for catching my failure to cast pointers before
printing. ]

#include <stdio.h>
#include <stdlib.h>

struct node
{ struct node *next;
int data;
};

struct node *add_lifo(struct node **list,int data)
{ struct node *new = malloc(sizeof *new);
if (!new) exit(EXIT_FAILURE);
new->next = *list;
new->data = data;
return *list = new;
}

int main(void)
{ int i;
struct node *list = NULL;
struct node *p;
for (i=0; i<6; i++) add_lifo(&list,i);
printf("\nList @ %p -%p\n\n",(void*)&list,(void*)list);
for (p=list; p; p=p->next)
{ printf("Node @ %p {%p %d}\n",(void*)p,(void*)p->next,p->data);
}
return 0;
}

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Jul 2 '06 #14
On 30 Jun 2006 23:53:47 -0700, "fool" <as******@yahoo.co.ukwrote:
>Dear group,

Thanks for all those who helped previously.

Now I get some value printed. Is that the value of the list getting
printed? Am I
really polluted the list?

Previous thread link (sorry for google)
http://groups.google.com/group/comp....8ac16a2ed4ed35

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

struct node *add_node(struct node *p, int funct_data)
{
struct node *t = p;
/* passed by values. So I save the argument value in a
* temp variable.Am I correct?*/
Since the very next statement changes the value of t, this
initialization is of no value.
>
t = malloc(sizeof *t);
if(!t)
{
printf("mem error");
exit(EXIT_FAILURE);
}
t->data = funct_data;
return t;
You have returned the address of the new node to the caller but you
have not linked this node to the list. You may have intended
p->next = t;
but that has problems as discussed below.
>}

int main(void)
{
struct node *p,*q;
int i;
q=malloc(sizeof *q);
q now points to an uninitialized block of memory aligned and sized for
holding a node.
> if(!q)
exit(EXIT_FAILURE);
The question here is why do you care if the malloc succeeded. You
never use the value in q to address a node. So why did you call
malloc in the first place?
> for(i=0 ; i< 6 ; i++)
{
q = add_node(p, i);
p is uninitialized. Its value is indeterminate. Any attempt to
evaluate it, such as being passed to a function, causes undefined
behavior.

Each time you execute this statement, the previous value in q gets
destroyed, causing a memory leak.
> printf("%d\n",q->data);
}
return 0;
}
In order to have a linked list, each node in the list except the last
must point to the next. In your case, this would be accomplished by
setting the next member of the parent node to the address of the new
node.

It is also common to use a pointer to identify the first node in a
list. Common approaches include a stand-alone pointer or a defined
node where only the next member is used.
Remove del for email
Jul 3 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: YoBro | last post by:
Hi I have used some of this code from the PHP manual, but I am bloody hopeless with regular expressions. Was hoping somebody could offer a hand. The output of this will put the name of a form...
4
by: D | last post by:
Hi folks, This may be pretty simple for you guys but it has me stumped. BTW I'm using Java 1.1, I know it's old, don't ask me why, I just have to. I have a long string in excess of 50k that I...
3
by: Franklin P Patchey | last post by:
I don't know anything about Javascript, but a little about standard HTML. I have created a website and have set the homepage to automatically play a sound file on opening. What I would like is...
1
by: Chris Dunaway | last post by:
I have a string: 555-1234,12345,"Jones, John" I want to split the string based on a comma as a delimiter. Since the name portion has a comma and is delimited with quotation marks, I need to...
14
by: Mat| | last post by:
Hello :-) I am learning XPath, and I am trying to get child nodes of a node whose names do *not* match a given string, e.g : <dummy> <example> <title>Example 1</title> <body>this is an...
2
by: Shankar | last post by:
Hi All, I completed my B.Sc. I want to settle as a s/w Tester that why I had learned Testing Tools. Please give ur suggestions to achieve my target. I dont have any experience in s/w Testing....
0
by: Steve Holden | last post by:
pierre-yves guido wrote: Your English is quite good. In future, though, please try to make your subject line say a bit more about the problem - we *all* need help! When you define a function or...
7
by: Bobh | last post by:
I am trying to figure out some code for a report issue; I have an employee who has a incentive scheme. I have a report showing various income amounts and in the report footer I vae totals for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.