473,626 Members | 3,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recursive structure?

I've come a across a program that declares the following data
structure.

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

It looks like a recursive data structure but I'm having trouble
understanding the use of it? Can somebody explain this?

How much memory will be allocated by the compiler when the structure is
instantiated?

The following structure makes use of the previous structure.

typedef struct queue {
node *head, *tail;
} queue;

Best regards!

Aug 9 '06 #1
20 8796
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
dspfun wrote:
I've come a across a program that declares the following data
structure.

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

It looks like a recursive data structure
Not really, but you /are/ close. First off, the quoted text isn't a
structure, it is a typedef. A typedef defines a name that will be
associated to a simple or complex storage type. In this case, the type
associated to the name "node' just happens to be a structure definition
called "node".
but I'm having trouble
understanding the use of it? Can somebody explain this?
Sure.

Assuming that you are concerned about the
struct node {
struct node *next;
};
portion of the typedef, then the structure definition called "node"
contains a /pointer/ (of the type "pointer to struct node").
How much memory will be allocated by the compiler when the structure is
instantiated?
First off, "instantiat ion" isn't the term to use. But, I know what you
mean.

How much memory?? Well, it will be
sizeof(struct node)
bytes long.

What's the "real" value, you ask? We can't tell you, other than to say
that it will be at least big enough to hold a pointer to a structure.
The structure /may/ be allocated with invisible padding/alignment
bytes, making the structure bigger than the sum of it's components, but
that's up to the compiler to determine. Also, depending on your
compiler, a pointer to struct node may be larger, smaller, or the same
size as any other pointer. Again, this is implementation dependant.

[snip]
HTH

- --
Lew Pitcher

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFE2fYCagV FX4UWr64RAsvqAK DoqBMdCETJhrKiF qbcTj1h7mwKBwCg 4MMf
esqUVrmtlZg6juK juZpIHV8=
=ERDL
-----END PGP SIGNATURE-----

Aug 9 '06 #2

Lew Pitcher wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
dspfun wrote:
I've come a across a program that declares the following data
structure.

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

It looks like a recursive data structure

Not really, but you /are/ close. First off, the quoted text isn't a
structure, it is a typedef. A typedef defines a name that will be
associated to a simple or complex storage type. In this case, the type
associated to the name "node' just happens to be a structure definition
called "node".
but I'm having trouble
understanding the use of it? Can somebody explain this?

Sure.

Assuming that you are concerned about the
struct node {
struct node *next;
};
portion of the typedef, then the structure definition called "node"
contains a /pointer/ (of the type "pointer to struct node").
Yes, this is exactly what I don't understand, how can "structure node"
contain a pointer of the type "pointer to struct node"? It seems that
"struct node" is not declared readily? How can the compiler know what
to do? To me it seems analogous to saying "x has the value x".
How much memory will be allocated by the compiler when the structure is
instantiated?

First off, "instantiat ion" isn't the term to use. But, I know what you
mean.

How much memory?? Well, it will be
sizeof(struct node)
bytes long.

What's the "real" value, you ask? We can't tell you, other than to say
that it will be at least big enough to hold a pointer to a structure.
The structure /may/ be allocated with invisible padding/alignment
bytes, making the structure bigger than the sum of it's components, but
that's up to the compiler to determine. Also, depending on your
compiler, a pointer to struct node may be larger, smaller, or the same
size as any other pointer. Again, this is implementation dependant.

[snip]
HTH

- --
Lew Pitcher

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFE2fYCagV FX4UWr64RAsvqAK DoqBMdCETJhrKiF qbcTj1h7mwKBwCg 4MMf
esqUVrmtlZg6juK juZpIHV8=
=ERDL
-----END PGP SIGNATURE-----
Aug 9 '06 #3

dspfun wrote:
Yes, this is exactly what I don't understand, how can "structure node"
contain a pointer of the type "pointer to struct node"? It seems that
"struct node" is not declared readily? How can the compiler know what
to do? To me it seems analogous to saying "x has the value x".
Ah, quite perceptive of you. But it does work because:

(1) What's included is a POINTER to a variable of the same type.

(2) On most every system I can think of, except perhaps some weird ol
Burroughs, a POINTER is just an address. And the address of anything,
no matter what the size, shape or type of the thing, the address is
just a fixed number of bits, often 16, 32, 48, or 64 bits.

(3) So when the compiler sees: struct foo{ foo * nextfoo; it goes
"okay, foo hasnt been fully declared yet, in fact, I may not even be
aware that there is a foo being declared, but a pointer to anything is
just 32 bits, I know how to allocate that"

(4) Eventually foo will come to an end and everything will be okay.

----

It can get even a bit more abstract, you can define ahead of time:

typedef foo * PtrToFoo;

and that will be accepted, as the compiler "knows" you may be intending
to do the same thing as above. of course eventually the base type has
to be defined somewheere.

Aug 9 '06 #4

"dspfun" <ds****@hotmail .comha scritto nel messaggio
news:11******** **************@ i3g2000cwc.goog legroups.com...
>
Lew Pitcher wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
dspfun wrote:
I've come a across a program that declares the following data
structure.
>
typedef struct node {
struct node *next;
} node;
>
It looks like a recursive data structure
Not really, but you /are/ close. First off, the quoted text isn't a
structure, it is a typedef. A typedef defines a name that will be
associated to a simple or complex storage type. In this case, the type
associated to the name "node' just happens to be a structure definition
called "node".
but I'm having trouble
understanding the use of it? Can somebody explain this?
Sure.

Assuming that you are concerned about the
struct node {
struct node *next;
};
portion of the typedef, then the structure definition called "node"
contains a /pointer/ (of the type "pointer to struct node").

Yes, this is exactly what I don't understand, how can "structure node"
contain a pointer of the type "pointer to struct node"? It seems that
"struct node" is not declared readily? How can the compiler know what
to do? To me it seems analogous to saying "x has the value x".
No. It is different.
If you say "struct x" I don't know the size, but
if you say (struct x *) I know the size, representation
and alignment, for each 'x'.

--
Giorgio Silvestri
DSP/Embedded/Real Time OS Software Engineer


Aug 9 '06 #5
dspfun wrote:

Lew Pitcher wrote:
>Assuming that you are concerned about the
struct node {
struct node *next;
};
portion of the typedef, then the structure definition called "node"
contains a /pointer/ (of the type "pointer to struct node").

Yes, this is exactly what I don't understand, how can "structure node"
contain a pointer of the type "pointer to struct node"?
Why could it not?
It seems that
"struct node" is not declared readily?
Doesn't matter: the compiler knows how to do a /pointer/ to /any/ struct.

If it said `struct node { struct node infinite; };` then there would
be a problem.

--
Chris "seeker" Dollin
A rock is not a fact. A rock is a rock.

Aug 9 '06 #6

Chris Dollin wrote:
dspfun wrote:

Lew Pitcher wrote:
Assuming that you are concerned about the
struct node {
struct node *next;
};
portion of the typedef, then the structure definition called "node"
contains a /pointer/ (of the type "pointer to struct node").
Yes, this is exactly what I don't understand, how can "structure node"
contain a pointer of the type "pointer to struct node"?

Why could it not?
It seems that
"struct node" is not declared readily?

Doesn't matter: the compiler knows how to do a /pointer/ to /any/ struct.

If it said `struct node { struct node infinite; };` then there would
be a problem.

--
Chris "seeker" Dollin
A rock is not a fact. A rock is a rock.
I understand now that the compiler knows how to do a pointer to /any/
struct.

But, if I declare:
node *mynode;

Then I have declared a pointer to a node struct. But all node struct
contains is a pointer to a node struct. I can't see anything useful
with this, however, since it is used it must be good for something.

Can someone give some example of what this (mynode pointer) is good
for?

Wouldn't it be equally good to declare node struct like this?:

struct node {
void *next;
};

Aug 9 '06 #7
dspfun (in 11************* *********@75g20 00...legro ups.com) said:

| Chris Dollin wrote:
|| dspfun wrote:
||
||
||| Lew Pitcher wrote:
|||| Assuming that you are concerned about the
||||| struct node {
||||| struct node *next;
||||| };
|||| portion of the typedef, then the structure definition called
|||| "node" contains a /pointer/ (of the type "pointer to struct
|||| node").
||||
|||
||| Yes, this is exactly what I don't understand, how can "structure
||| node" contain a pointer of the type "pointer to struct node"?
||
|| Why could it not?
||
||| It seems that
||| "struct node" is not declared readily?
||
|| Doesn't matter: the compiler knows how to do a /pointer/ to /any/
|| struct.
||
|| If it said `struct node { struct node infinite; };` then there
|| would
|| be a problem.
|
| I understand now that the compiler knows how to do a pointer to
| /any/ struct.
|
| But, if I declare:
| node *mynode;
|
| Then I have declared a pointer to a node struct. But all node struct
| contains is a pointer to a node struct. I can't see anything useful
| with this, however, since it is used it must be good for something.
|
| Can someone give some example of what this (mynode pointer) is good
| for?

It's not unusual to see something like:

node *head; /* Pointer to first element */
node *tail; /* Pointer to final element */

or even:

struct
{ node *head;
node *tail;
} mylist;

to provide a "root" for a list.

| Wouldn't it be equally good to declare node struct like this?:
|
| struct node {
| void *next;
| };

Certainly.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Aug 9 '06 #8
Morris Dovey (in 44************* **********@news .qwest.net) said:

| dspfun (in 11************* *********@75g20 00...legro ups.com)
| said:
|
|| struct node {
|| void *next;
|| };
|
| Certainly.

Oops! I missed the 'void'. It would _not_ be better.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Aug 9 '06 #9

Morris Dovey wrote:
dspfun (in 11************* *********@75g20 00...legro ups.com) said:

| Chris Dollin wrote:
|| dspfun wrote:
||
||
||| Lew Pitcher wrote:
|||| Assuming that you are concerned about the
||||| struct node {
||||| struct node *next;
||||| };
|||| portion of the typedef, then the structure definition called
|||| "node" contains a /pointer/ (of the type "pointer to struct
|||| node").
||||
|||
||| Yes, this is exactly what I don't understand, how can "structure
||| node" contain a pointer of the type "pointer to struct node"?
||
|| Why could it not?
||
||| It seems that
||| "struct node" is not declared readily?
||
|| Doesn't matter: the compiler knows how to do a /pointer/ to /any/
|| struct.
||
|| If it said `struct node { struct node infinite; };` then there
|| would
|| be a problem.
|
| I understand now that the compiler knows how to do a pointer to
| /any/ struct.
|
| But, if I declare:
| node *mynode;
|
| Then I have declared a pointer to a node struct. But all node struct
| contains is a pointer to a node struct. I can't see anything useful
| with this, however, since it is used it must be good for something.
|
| Can someone give some example of what this (mynode pointer) is good
| for?

It's not unusual to see something like:

node *head; /* Pointer to first element */
node *tail; /* Pointer to final element */

or even:

struct
{ node *head;
node *tail;
} mylist;

to provide a "root" for a list.
I'm probably missing something here because I don't see what the
purpose of such a list would be, all it is is a list of node pointers,
isn't it? What is the use of such a list? Wouldn't one like to have
some data at each entry of a list?
| Wouldn't it be equally good to declare node struct like this?:
|
| struct node {
| void *next;
| };

Certainly.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Aug 9 '06 #10

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

Similar topics

2
2006
by: Kaptain524 | last post by:
Hello, I am using PHP 5.0.4 with Apache 2, on WinXP Pro. This behavior appears to be fundamental however, and should not be affected by platform. It would seem that there is some kind of bug in the process that creates the reference when it is being assigned to an array element within itself. If it is already referenced, it just assigns the existing reference and avoids the problem.
7
2653
by: aurora | last post by:
I love generator and I use it a lot. Lately I've been writing some recursive generator to traverse tree structures. After taking closer look I have some concern on its performance. Let's take the inorder traversal from http://www.python.org/peps/pep-0255.html as an example. def inorder(t): if t: for x in inorder(t.left):
7
567
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
25
2815
by: Mike MacSween | last post by:
Regular viewers may want to turn off now. This will be an orchestral management system. Musicians and other staff being booked/paid for jobs. A job may contain other jobs, e.g: World Tour contains US leg and Europe leg (and others) US leg contains State tours (and others)
5
1883
by: brett valjalo | last post by:
Hey Gang! SORRY ABOUT THE LENGTH! Nice to see some of the same faces around this place from way back when ... Whatta buncha CDMA addicts some o' y'all are ;) Don't get me wrong, I understand, believe me! Took me a lot of therapy, various 12-step programmes, interventions by loved ones, etc, but I finally managed to excavate, er, extricate myself from the
9
8318
by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3) and two folders (Subfolder 1, Subfolder 2). .......I need to delete File1, File2, File3. Subfolder 1 contains FileA. .......Need to delete FileA. Subfolder 2 contains FileB, FileC, FileD.
1
4366
by: ozcanseker | last post by:
I am trying to write a product-row material cost program. Every product consists of row materials. When I sum up cost of row materials of each product I can find cost of products. But when the row material of the a product is a row material again the my solution does not work. My table is like that: Product Row material p1 r1 p1 r2 p1 r3 p2 r4 p2 p1
4
8911
by: so.intech | last post by:
for example, ret = 0; for(i=0; i<3; i ++;) { for(j=0; j<4; j++;) { for(k=0; k<3; k++;) { for(m=0; m<4; m++;) {
3
1326
by: mizrandir | last post by:
Can someone tell me why python doesn't crash when I do the following: ] ] How does python handle this internally? Will it crash or use up lot's of memory in similar but more complicated cases?
0
8268
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8202
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8510
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6125
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.