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

Breaking a node of link list

i've following union

union header
{
struct
{
unsigned int size;
union header *next;
}s;

long x;
}

typedef union header Header;

i use Header as follows :
Header *ptr_free = (Header *)sbrk(4096);
Header *ptr_alloc = NULL;

================================================== ====================

Now, ptr_free has size = 4096 bytes,

i want to break this node ( ptr_free ) into two nodes :
one is pointed by ptr_alloc and the remaining part by ptr_free. and in
each pointer the size must be maintained.

How can i do it??
plz give me a hint.

Jul 12 '07 #1
6 1646
In article <11*********************@r34g2000hsd.googlegroups. com>,
Nehil <ne***********@gmail.comwrote:
>i've following union
>union header
{
struct
{
unsigned int size;
union header *next;
}s;
long x;
}
>typedef union header Header;
>i use Header as follows :
Header *ptr_free = (Header *)sbrk(4096);
Header *ptr_alloc = NULL;
>Now, ptr_free has size = 4096 bytes,
It does??

Quoting from the Linux 2.4 sbrk() man page:

brk and sbrk are not defined in the C Standard and are deliberately
excluded from the POSIX.1 standard (see paragraphs B.1.1.1.3 and
B.8.3.3)

So here in comp.lang.c we don't know what the heck sbrk(4096) does.
And what it does might vary between different systems (that bother
to implement it at all) and between different versions of the same system.
So if your question depends upon some particular result being
returned by sbrk() then you need to take the question to a newsgroup
that deals with the OS or OSes that you need this to be portable to.

>i want to break this node ( ptr_free ) into two nodes :
one is pointed by ptr_alloc and the remaining part by ptr_free. and in
each pointer the size must be maintained.
>How can i do it??
plz give me a hint.
No can do unless you are the implementor of the processor architecture.
There are very few systems in which a pointer contains a size
*within the pointer itself* -- it would require a pointer representation
wider than the number of bits required to describe the largest object
size pointed to, and you would get wacky pointer semantics if you
did pointer arithmetic (and remember that pointer arithmetic must
be reversable...).

It would be possible to maintain the size of the nodes at the
location pointed *to*, but not practical in the pointer -itself-
("and in each pointer the size must be maintained").

Your union header has a size field, but you seem to be lacking
a field for the remaining storage in the node. Of course you have
a semantic problem in defining the type for that storage if you
are working in C89; in C99 you could type it as a VLA (Variable
Length Array).
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Jul 12 '07 #2
On Jul 12, 9:39 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
In article <1184256137.465498.17...@r34g2000hsd.googlegroups. com>,

Nehil <nehilparas...@gmail.comwrote:
i've following union
union header
{
struct
{
unsigned int size;
union header *next;
}s;
long x;
}
typedef union header Header;
i use Header as follows :
Header *ptr_free = (Header *)sbrk(4096);
Header *ptr_alloc = NULL;
Now, ptr_free has size = 4096 bytes,

It does??

Quoting from the Linux 2.4 sbrk() man page:

brk and sbrk are not defined in the C Standard and are deliberately
excluded from the POSIX.1 standard (see paragraphs B.1.1.1.3 and
B.8.3.3)

So here in comp.lang.c we don't know what the heck sbrk(4096) does.
And what it does might vary between different systems (that bother
to implement it at all) and between different versions of the same system.
So if your question depends upon some particular result being
returned by sbrk() then you need to take the question to a newsgroup
that deals with the OS or OSes that you need this to be portable to.
i want to break this node ( ptr_free ) into two nodes :
one is pointed by ptr_alloc and the remaining part by ptr_free. and in
each pointer the size must be maintained.
How can i do it??
plz give me a hint.

No can do unless you are the implementor of the processor architecture.
There are very few systems in which a pointer contains a size
*within the pointer itself* -- it would require a pointer representation
wider than the number of bits required to describe the largest object
size pointed to, and you would get wacky pointer semantics if you
did pointer arithmetic (and remember that pointer arithmetic must
be reversable...).

It would be possible to maintain the size of the nodes at the
location pointed *to*, but not practical in the pointer -itself-
("and in each pointer the size must be maintained").

Your union header has a size field, but you seem to be lacking
a field for the remaining storage in the node. Of course you have
a semantic problem in defining the type for that storage if you
are working in C89; in C99 you could type it as a VLA (Variable
Length Array).
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
================================================== =================

well, i agree that sbrk(int) is implementation defined, but don't u
think malloc must be using *something* like sbrk or brk to request the
memory chunk from OS.

or

do u know some poratble way of taking memory from OS? please tell me.

Now, let say i have a link list as follows :

header-->NODE1 =NODE2 =NODE3 =NODE4 =NULL

is there any way to merge the two nodes or to break a node in two
parts?

Please clarify.
Thanks.

Jul 12 '07 #3
Nehil wrote:
On Jul 12, 9:39 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
In article <1184256137.465498.17...@r34g2000hsd.googlegroups. com>,
Nehil <nehilparas...@gmail.comwrote:
>
>i've following union
>union header
>{
struct
{
unsigned int size;
union header *next;
}s;
long x;
>}
>typedef union header Header;
>i use Header as follows :
Header *ptr_free = (Header *)sbrk(4096);
Header *ptr_alloc = NULL;
>Now, ptr_free has size = 4096 bytes,
It does??

Quoting from the Linux 2.4 sbrk() man page:

brk and sbrk are not defined in the C Standard and are deliberately
excluded from the POSIX.1 standard (see paragraphs B.1.1.1.3 and
B.8.3.3)

So here in comp.lang.c we don't know what the heck sbrk(4096) does.
And what it does might vary between different systems (that bother
to implement it at all) and between different versions of the same system.
So if your question depends upon some particular result being
returned by sbrk() then you need to take the question to a newsgroup
that deals with the OS or OSes that you need this to be portable to.
<snip>
well, i agree that sbrk(int) is implementation defined, but don't u
think malloc must be using *something* like sbrk or brk to request the
memory chunk from OS.
Not necessarily. There are situations where a host operating system
might not exist at all. In such cases malloc and friends would
presumably use system memory directly.

But the point is, whatever method malloc uses is system specific. C
runs on a huge variety of systems and hence it would not be feasible
for this group to discuss the details of all these systems. Hence only
Standard C is discussed here. Most major target platforms for C have
their own dedicated group, as do most major C compilers.
or

do u know some poratble way of taking memory from OS? please tell me.
Use malloc or calloc or realloc. Use free to deallocate.
Now, let say i have a link list as follows :

header-->NODE1 =NODE2 =NODE3 =NODE4 =NULL

is there any way to merge the two nodes or to break a node in two
parts?
Why do you need to do this? Nodes may exist at different memory
locations. What is it that you're doing that calls for "breaking" a
node or merging them?

Jul 12 '07 #4
Nehil wrote:
i've following union

union header
{
struct
{
unsigned int size;
union header *next;
}s;

long x;
}

typedef union header Header;

i use Header as follows :
Header *ptr_free = (Header *)sbrk(4096);
Header *ptr_alloc = NULL;

Now, ptr_free has size = 4096 bytes,

i want to break this node ( ptr_free ) into two nodes :
one is pointed by ptr_alloc and the remaining part by ptr_free. and in
each pointer the size must be maintained.
You cannot store sizes within pointers in Standard C. What you can do
is set ptr_free and ptr_alloc to point to the appropriate offsets
within the node and store the sizes is seperate variables. You could
encapsulate the pointer and it's associated size variable into a
structure. You can use the offsetof operator to get the offset of the
member where you want to "break" the node.

Why do you want to do this. This is probably going to be non-portable,
particularly if you try to access the separate parts of the union.

Jul 12 '07 #5
On Jul 12, 11:45 pm, santosh <santosh....@gmail.comwrote:
Nehil wrote:
i've following union
union header
{
struct
{
unsigned int size;
union header *next;
}s;
long x;
}
typedef union header Header;
i use Header as follows :
Header *ptr_free = (Header *)sbrk(4096);
Header *ptr_alloc = NULL;
Now, ptr_free has size = 4096 bytes,
i want to break this node ( ptr_free ) into two nodes :
one is pointed by ptr_alloc and the remaining part by ptr_free. and in
each pointer the size must be maintained.

You cannot store sizes within pointers in Standard C. What you can do
is set ptr_free and ptr_alloc to point to the appropriate offsets
within the node and store the sizes is seperate variables. You could
encapsulate the pointer and it's associated size variable into a
structure. You can use the offsetof operator to get the offset of the
member where you want to "break" the node.

Why do you want to do this. This is probably going to be non-portable,
particularly if you try to access the separate parts of the union.
================================================== =================

I've the following requirement :

i'm developing a Garbage Collector for C. so i'd do develop a complete
memory manager ie i'd to allocate and de-allocate. for allocation i'm
using sbrk(int mem).
acc. to man page it returns a void pointer to the memory chunk of mem
bytes. but actually it is returning a void pointer to a memory chunk
of 4kb (4096 bytes).
if the argument of sbrk is in between 1 and 4096 (including both ends)
the size of the chunk returned will be 4096 bytes.

Now, at the start of program this whole chunk can be treated as a free
block of memery. so i do as follows :

Header *ptr_free = (Header *)sbrk(4096);
Header *ptr_alloc = NULL;

when, user requests for memory of certain bytes, let say 20 byets. so
i do as follows : (i'm considering only the very 1st request)

ptr_alloc = ptr_free;
ptr_free = ptr_free + 20 + sizeof(Header);
void *memory = ptr_alloc + sizeof(Header);
return memory;

i.e. ptr_free is now pointing to the start of un-allocated part of
that big chunk. and memory points to the allocated part, but leaves
the starting header.

Now, ptr_free should have size of "rest of the block". but it is a
single node so it's not possible. plz correct me if i'm wrong.
To keep the record of free blocks i need to break the big chunk into
parts : Allocated and Free.
How can i break the node into two parts so that it can have size
information for allocated and free parts.

Hope u understand my question and problem. :-)
Thanks.

Jul 12 '07 #6
In article <11**********************@22g2000hsm.googlegroups. com>,
Nehil <ne***********@gmail.comwrote:
>I've the following requirement :
>i'm developing a Garbage Collector for C. so i'd do develop a complete
memory manager ie i'd to allocate and de-allocate. for allocation i'm
using sbrk(int mem).
I am not clear what your portability requirements are? A number
of pointer operations which are formally unspecified or undefined
behaviour in C are well defined and simple if you are allowed to
assume particular operating environments -- but can be a large
large nuisance if you are trying to work portably.
>acc. to man page it returns a void pointer to the memory chunk of mem
bytes. but actually it is returning a void pointer to a memory chunk
of 4kb (4096 bytes).
I'd like to emphasize that sbrk() is not considered portable,
being not a part of C nor of POSIX.1. It is part of the
Single Unix Specification Version 2, but there are still quite
a number of useful Unix operating systems which are not certified
to Version 2 of the SUS. Even the SUS lists them as LEGACY and says,
"The use of malloc() is now preferred because it can be used
portably with all other memory allocation functions and with any
function that uses other allocation functions."

http://www.opengroup.org/onlinepubs/...9/xsh/brk.html

"It is unspecified whether the pointer returned by sbrk() is
aligned suitably for any purpose."

Which is equivilent to a flaming red arrow saying THIS IS NOT PORTABLE.

>Now, at the start of program this whole chunk can be treated as a free
block of memery. so i do as follows :
Header *ptr_free = (Header *)sbrk(4096);
Header *ptr_alloc = NULL;
>when, user requests for memory of certain bytes, let say 20 byets. so
i do as follows : (i'm considering only the very 1st request)
And if the user requests 4096 - sizeof(struct header) or more,
you have problems. The memory your allocation routines hands over
to the users must be consequative, so you cannot hand over multiple
chunks each with an embedded header.

>ptr_alloc = ptr_free;
ptr_free = ptr_free + 20 + sizeof(Header);
void *memory = ptr_alloc + sizeof(Header);
return memory;
>i.e. ptr_free is now pointing to the start of un-allocated part of
that big chunk. and memory points to the allocated part, but leaves
the starting header.
>Now, ptr_free should have size of "rest of the block". but it is a
single node so it's not possible. plz correct me if i'm wrong.
To keep the record of free blocks i need to break the big chunk into
parts : Allocated and Free.
How can i break the node into two parts so that it can have size
information for allocated and free parts.
If you do not have enough C experience to figure this out on your
own (and it is not difficult) then you do not have enough C experience
to implement a garbage collector. This bit is trivial compared to
figuring out whether a particular block of memory is still in
use or not!
--
Programming is what happens while you're busy making other plans.
Jul 12 '07 #7

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

Similar topics

10
by: Fabio | last post by:
Hi everyone, Is there anybody who can suggest me a link where I can find information about 'Persistent linked list' ? I need to implement a linked list where every node is a structure like the...
27
by: The Bicycling Guitarist | last post by:
Hi. I found the following when trying to learn if there is such a thing as a non-breaking hyphen. Apparently Unicode has a ‑ but that is not well-supported, especially in older browsers. Somebody...
4
by: Sumika | last post by:
Hello, I'm a newbie here, so don't know much friends. I've problem deleting my node at the tail, so could you all help me to solve my error,I worked on it for quite sometime but it just can't...
2
by: mathon | last post by:
hello, i thought i create a new topic for that because i deal with a certain problem. I have implemented all methods for a LinkdeList (for my sequence class) and tested all correct. Now i only...
7
by: spidey12345 | last post by:
can anybody help me with this i have a baddata.txt file that has certain data like this: " blah blah ere werew ss a s ef df ww erew asf" and i...
1
by: ahoway | last post by:
I am having problems deleting a node from a link list. I need to delete the node which contains the number six. This is what I have so far..... Thank you in advance. #include <iostream>...
11
by: hotice | last post by:
How to write code to delete a specific node in a single link list that takes O(1) time? £¨ link list uses pointers, not hash. £© That is, the time deleting a node is the same (independent from...
3
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...
10
by: Aditya | last post by:
Hi All, I would like to know how it is possible to insert a node in a linked list without using a temp_pointer. If the element is the first element then there is no problem but if it is in...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
1
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...
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
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
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...

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.