473,394 Members | 1,889 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,394 software developers and data experts.

SImple question about structure and linked list

Hi all,

Can a linked list be a member of a structure?
If so, when I add or remove an element from the linked list, the size
of the structure will change. Will it cause any problem?

Thanks a lot.

John
Jul 22 '05 #1
5 3218
jo*********@yahoo.com (John) wrote in
news:c3**************************@posting.google.c om:
Hi all,

Can a linked list be a member of a structure?
Yes, e.g.

struct S {
std::list<int> intList;
};
If so, when I add or remove an element from the linked list, the size
of the structure will change.
No it won't. The elements added to the list are not stored directly in
the list object. They are stored in dynamically allocated memory that is
referred to by the list object.
Will it cause any problem?


See above.

Gregg
Jul 22 '05 #2
John wrote:
Can a linked list be a member of a structure?
No. But a structure may contain a reference (pointer)
to the head of a linked list.
If so, when I add or remove an element from the linked list,
the size of the structure will change.
No. The size of a struct[ure] is fixed.
Will it cause any problem?


A C++ struct (class) does not necessarily *contain* an object.
It may contain references (pointers) to sub objects which are considered
part of the object but *not* part of the struct (class).
Jul 22 '05 #3
John wrote:
Hi all,

Can a linked list be a member of a structure?
Whenever you refer to a list, you refer to the head of the list. So
it is perfectly valid to have a list as a member of the structure.
If so, when I add or remove an element from the linked list, the size
of the structure will change. Will it cause any problem?


Assume you have a struct linked_list already defined.

typedef struct Node {
int info;
struct Node * next;
} NODE;

typedef struct {
NODE * head;
} MyDataStructure;
The size of a variable of type 'MyDataStructure' is fixed, as you could
see. It does not depend on the list at all. ( It is equal to the size of
a single pointer - depending on your implementation - compiler / arch ).

HTH
--
Karthik.
Humans please 'removeme_' for my real email.
Jul 22 '05 #4
John wrote:
Hi all,

Can a linked list be a member of a structure?
If so, when I add or remove an element from the linked list, the size
of the structure will change. Will it cause any problem?

Thanks a lot.

John


Consider the following structure:

struct EXAMPLE
{
char *s ;
} ;

What is the size of this structure? It depends on your implementation,
of course, but for sake of argument let's say that pointers require 4
bytes of memory, and the compiler doesn't introduce any sort of overhead
for structs (a reasonable assumption for a standard PC/compiler).

So, in that case, we have that sizeof(EXAMPLE) == 4.

Now, let's allocate a big chunk of memory.

EXAMPLE e ;
e.s = new char [1024] ;

Now, what is the size of 'e'? Well, we didn't actually do anything to
change the size of e. We allocated 1024 characters *somewhere*, and we
saved the memory address at which we allocated them in e.s, but we block
of memory that represents 'e' didn't change size. So sizeof(e) ==
sizeof(EXAMPLE) == 4.

strings, lists, vectors, and in fact any other sort of container you
could find or dream up will do essentially this same thing. As it needs
more memory, it will allocate it dynamically *somewhere*, but all it
will keep is a pointer to where it allocated that memory. In fact,
there isn't actually anything you CAN do to change the size of a
structure. It turns out that sizeof() really isn't even a function at
all, but instead gets evaluated by the compiler at compile time.

Now consider a second structure:
struct EXAMPLE2
{
int i ;
std::vector<int> v ;
std::string s ;
std::list<int> l ;
} ;

How big is this structure? Well, that depends on what exactly the
members of a vector, string, and list are. On my particular compiler
that structure requires 24 bytes. Now I can add and remove things from
the vector, string, and list as much as I like and still be safe in the
knowledge that the size of the structure is 24 bytes. That doesn't mean
that more memory isn't getting allocated and deallocated somewhere. It
is, and part of the 24 bytes that make up my structure are pointers to
all that memory, but it will never change the fact that my structure is
only 24 bytes.

Alan
Jul 22 '05 #5
Thanks Alan. I understand now.

John

Alan Johnson <al****@mailandnews.com> wrote in message news:<40********@news.ua.edu>...
John wrote:
Hi all,

Can a linked list be a member of a structure?
If so, when I add or remove an element from the linked list, the size
of the structure will change. Will it cause any problem?

Thanks a lot.

John


Consider the following structure:

struct EXAMPLE
{
char *s ;
} ;

What is the size of this structure? It depends on your implementation,
of course, but for sake of argument let's say that pointers require 4
bytes of memory, and the compiler doesn't introduce any sort of overhead
for structs (a reasonable assumption for a standard PC/compiler).

So, in that case, we have that sizeof(EXAMPLE) == 4.

Now, let's allocate a big chunk of memory.

EXAMPLE e ;
e.s = new char [1024] ;

Now, what is the size of 'e'? Well, we didn't actually do anything to
change the size of e. We allocated 1024 characters *somewhere*, and we
saved the memory address at which we allocated them in e.s, but we block
of memory that represents 'e' didn't change size. So sizeof(e) ==
sizeof(EXAMPLE) == 4.

strings, lists, vectors, and in fact any other sort of container you
could find or dream up will do essentially this same thing. As it needs
more memory, it will allocate it dynamically *somewhere*, but all it
will keep is a pointer to where it allocated that memory. In fact,
there isn't actually anything you CAN do to change the size of a
structure. It turns out that sizeof() really isn't even a function at
all, but instead gets evaluated by the compiler at compile time.

Now consider a second structure:
struct EXAMPLE2
{
int i ;
std::vector<int> v ;
std::string s ;
std::list<int> l ;
} ;

How big is this structure? Well, that depends on what exactly the
members of a vector, string, and list are. On my particular compiler
that structure requires 24 bytes. Now I can add and remove things from
the vector, string, and list as much as I like and still be safe in the
knowledge that the size of the structure is 24 bytes. That doesn't mean
that more memory isn't getting allocated and deallocated somewhere. It
is, and part of the 24 bytes that make up my structure are pointers to
all that memory, but it will never change the fact that my structure is
only 24 bytes.

Alan

Jul 22 '05 #6

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

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
2
by: John | last post by:
Thanks a lot. How about "Vector"? If I put a Vector in a structure, the size of the structure may dynamically change, right? John Gregg <gregg@invalid.invalid> wrote in message...
5
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to...
13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
1
by: Tim | last post by:
I can't seem to figure out why this very simple linked list wont build.. I mean, there is no intelligence, just add to end. Anyway, please let me know if something i can do will make head (the...
3
by: bjhecht | last post by:
http://rafb.net/paste/results/tJoB4z75.html After executing the following code I receive the errors: a.c: In function `insert': a.c:59: error: incompatible types in assignment I have no...
9
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node *...
4
by: phe2003 | last post by:
Hi All, I've been testing some extremely simple code about a linked list. What I keep doing is actually writing some parts of a linked list and testing them. Below are my codes:...
3
by: quorn | last post by:
Hi there, I have an assignment in which the first part requires me to store coordinates in a .txt file in a linked list data structure. The text file has coordinates laid out as follows: 1 1 5...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...
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...

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.