RE: Linked lists in C and C++.

  • From: "Sina Bahram" <sbahram@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Wed, 10 Oct 2007 20:43:23 -0400

Laura handled this, but I wanted to point out that the compiler does know
how long to make any pointer on your system, and that's the bit width of
your processor architecture, such as 32 bit or 64 bit, so it's ok. Now, if
you tried to have a local variable of that struct's type, instead of a
pointer, you'd be in trouble, since that would be undefined, or rather
recursively defined.

Take care,
Sina 

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Graham Hardy
Sent: Wednesday, October 10, 2007 7:38 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Linked lists in C and C++.

Hi all,

I am creating a linked list in a C++ program, which I can envision as
follows.

typedef struct
{
 /* ... */
 token_t *next;
}
  token_t;

(Yes, this list only needs to be linked in one direction.) Clearly the
problem is that when I declare the pointer to the next object, though it is
a pointer of the type of the members in my list, since that type has not
been declared yet, I don't think I can do that. My first instinct told me to
use void pointers. Alternatively, I suppose I could declare the struct
giving the typename after the keyword 'struct' and before the '{', as,
struct token_t { /* ... */ }; Does this method change anything in the way
that types are concerned? That is, what is the diference between struct A
{/*...*/}; and typedef struct {/*...*/} B;?

And one more question. Despite the 'size' of a given data type being
explicit (as, say, a char being 1 byte), I am led to believe that where the
actual heap is concerned the alignment is not quite the same, such as 4 or 8
bytes for a char. I believe one can use the directives such as--

#pragma pack (...)

or something akin to that. If so, how does the syntax of this pack function
work? It seems that it begins with a keyword of some sort, as to whether the
previous alignment should be retained for the future, and then a new value.
Could someone enlighten me?

Thanks in advance,

Graham.

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: