473,405 Members | 2,160 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,405 software developers and data experts.

struct declaration and using free()

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

Similar topics

14
by: Gunnar G | last post by:
Hello. My compiler (GCC 3.3.*) does not complain about the following: #include <iostream> #include <vector> using namespace std; struct X{ int a,b,c; vector<X> pp;
11
by: Tim | last post by:
Why won't the declaration of a struct array work if I do it like this: 1 typedef struct 2 { 3 int pens; 4 int pencils; 5 } Stationers; 6 .........
3
by: Emanuele Blanco | last post by:
Hi there, I just compiled a program that uses linked lists (needed it as an homework for my Programming course at University). It works flawlessly, even if I notice a thing. Here's my linked...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
15
by: dutchgoldtony | last post by:
Hi all, I was just wondering if this is possible. I'm trying to implement a viterbi decoder in C and am creating an array of nodes (the struct), and an array of pointers to nodes (the member...
4
by: janssenssimon | last post by:
//de structure om de highscores in op de slagen typedef struct score{ char *naam; int veld; int score; struct score *volg; }HIGH; void toonhighscores(void)
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
2
by: Laurent Deniau | last post by:
I would like to know why the following small program does not compile (checked with gcc 4.1.2) and if the compiler behavior is correct: struct A; typedef void (T)(struct A*); void f(void) {...
21
by: heavyz | last post by:
In C, if i want to declare a struct, there are 3 methods to do so: 1: struct dummy { ... }; 2: typedef struct { ... } dummy; 3: typedef struct _dummy { ... } dummy; Usually i use the first...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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.