473,496 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

About allocating memory to a node in a link list

When you create node 1 you allocate memory and link it Again when you
create node 2 you would allocate memory for that node in a different
section of the code. Is there more efficient way where I can allocate
memory once and use it to create new node in list.
-Thanks
-Kane
Nov 13 '05 #1
1 3006
Kane wrote:

When you create node 1 you allocate memory and link it Again when you
create node 2 you would allocate memory for that node in a different
section of the code. Is there more efficient way where I can allocate
memory once and use it to create new node in list.


I'm not sure what you're asking, but I'll make
an attempt at answering anyhow ...

If the linked list has two nodes, those two nodes
must occupy two chunks of memory. Three nodes require
three chunks, N nodes require N chunks. You might be
able to play strange games by having the various chunks
overlap each other partially, but that way lies madness.

The existence of two or three or N chunks doesn't
necessarily imply two or three or N separate allocations.
You could, for example, use malloc() to reserve enough
memory for twenty nodes and then dole them out one by
one internally; this technique has some advantages when
the nodes are very small and very numerous. However, you
cannot then free() or realloc() the individual nodes;
you can only use free() or realloc() on the original
twenty-node block as a whole.

The existence of multiple allocated blocks (whether
of one or many nodes each) doesn't imply the presence
of multiple malloc() calls in your code. You can (and
usually should) arrange to execute and re-execute and
re-re-execute a single malloc() call as many times as
needed. Sometimes it's convenient to treat the very
first node specially and use an executed-only-once
malloc() call for it, but you should almost always have
just a single malloc() for every subsequent block.

If that doesn't answer your question, try restating
it more clearly.

--
Er*********@sun.com
Nov 13 '05 #2

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

Similar topics

4
1669
by: John | last post by:
Hi all: I write the following code about vector and list. Why the result is not correct? Thanks. John --------------------------------------------------------------
4
745
by: Sameer | last post by:
Hello Group, This is one problem in programming that is troubling me. there is a segmentation fault just before creating memory to a structure ..i.e, just after the "allocating memory "...
50
2786
by: Joseph Casey | last post by:
Greetings. I have read that the mistake of calling free(some_ptr) twice on malloc(some_data) can cause program malfunction. Why is this? With thanks. Joseph Casey.
19
3781
by: allanallansson | last post by:
Hi i would like some guidelines for a experiment of mine. I want to experiment with the swap and ctrl-z in linux. And for this i want to create a c program that allocates almost all the free memory...
10
2177
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
9
2816
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 *...
3
447
by: Kane | last post by:
When you create node 1 you allocate memory and link it Again when you create node 2 you would allocate memory for that node in a different section of the code. Is there more efficient way where I...
12
2132
by: filia&sofia | last post by:
For example I would like to dynamically allocate memory for linked list (or complex struct etc.) that has not maximum number of elements. All that I can remember is that I have to have allocated...
8
2530
by: pereges | last post by:
Hi, I'm trying to deallocate a kd tree which I created dynamically. There were no problems creating the structure and I can access it easily but there is a problem while trying to free it. Here's...
19
3359
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
I am currently designing a synchronized queue used to communicate between threads. Is the code given below a good solution? Am I using mutex lock/unlock more than needed? Are there any resources...
0
6991
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
7160
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
7196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7373
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...
1
4897
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...
0
3088
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
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 ...
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.