struct declaration and using free()

D

dam_fool_2003

hai,
Consider this following fragement

typedef struct node
{
...
...
...
struct node *next;
}NODE;

node *list;
NODE *link;
after some operation on the object list the program uses free(link) to
free mem.
I have two questions:
1) The object link alone get de-allocated or the along with the object
list? Or put it in this way , will it both get deleted?
2) What is the advantage of declaring the struct like this in a long
programs?
 
J

Jens.Toerring

hai,
Consider this following fragement
typedef struct node
{
...
...
...
struct node *next;
}NODE;
node *list;

No declaration of what "node" is in scope, so this will result in a
syntax error.
NODE *link;
after some operation on the object list the program uses free(link) to
free mem.
I have two questions:
1) The object link alone get de-allocated or the along with the object
list? Or put it in this way , will it both get deleted?

When you call "free(link)" only the memory 'link' is pointing to will get
free'ed, nothing else. If what 'link' pointed to contained other pointers
to allocated memory you lost that pointers and you won't be unable to free
the memory, creating a memory leak.
2) What is the advantage of declaring the struct like this in a long
programs?

Because that's the way how you normally create linked lists, no matter
if the program is short or long. Do you have found a better method?

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
D

dam_fool_2003

No declaration of what "node" is in scope, so this will result in a
syntax error.



When you call "free(link)" only the memory 'link' is pointing to will get
free'ed, nothing else. If what 'link' pointed to contained other pointers
to allocated memory you lost that pointers and you won't be unable to free
the memory, creating a memory leak.

#include<XXX_.h>
int main(void)
{
int *a,b=90,c=20;
a = malloc(sizeof *a);
if(!a)
exit(EXIT_FAILURE);
else
{
a = &c;
*a = b;
printf("before calling free() %d %d %d\n",*a,b,c);
free(a);
printf("after calling free() %d %d %d\n",*a,b,c);
}
return 0;
}
OUTPUT:
before calling free() 90 90 90
after calling free() 8 -1 8
Sir, in the above code the *a is referred and made it to point to b.
When I free *a all three (a,b,c)
was deallocated. Is that what the explanation means?

Because that's the way how you normally create linked lists, no matter
if the program is short or long. Do you have found a better method?

My god, if I have a better method then I will be clearing the doubts
in c.l.c instead of asking!!
Ok I must make my self clear before I post. I will have a new thread
and clear one.
Thanks for the answers.
 
J

Joe Wright

#include<XXX_.h>
int main(void)
{
int *a,b=90,c=20;
a = malloc(sizeof *a);
if(!a)
exit(EXIT_FAILURE);
else
{
a = &c;
^^^^^^^
You have now lost reference to allocated memory. a points to c.
*a = b; Now c == b == 90
printf("before calling free() %d %d %d\n",*a,b,c); Equivalent to printing c,b,c.
free(a);
^^^^^^^
Bingo! a doesn't point to allocated memory, it points to c. From here
on, absolutely anything can happen. You have committed Undefined
Behavior! The punishment for the crime of UB Implementation Defined. :)
 

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

No members online now.

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top