By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,083 Members | 2,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,083 IT Pros & Developers. It's quick & easy.

struct declaration and using free()

P: n/a
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?
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
da***********@yahoo.com wrote:
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
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #2

P: n/a
Je***********@physik.fu-berlin.de wrote in message news:<bl************@uni-berlin.de>...
da***********@yahoo.com wrote:
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.


#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?

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?


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.
Regards, Jens

Nov 13 '05 #3

P: n/a
da***********@yahoo.com wrote:

Je***********@physik.fu-berlin.de wrote in message news:<bl************@uni-berlin.de>...
da***********@yahoo.com wrote:
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.


#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. :-)
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?
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?


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.
Regards, Jens


--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.