extend/shrink heap ?

  • Thread starter Liu, Qingzhu [CAR:2N84:EXCH]
  • Start date
L

Liu, Qingzhu [CAR:2N84:EXCH]

From memory management perspective, how to code in c to create a managed
heap
that can be extended and shrunk ?
Thanks for help in advance.

Qingzhu
 
A

Artie Gold

Liu said:
From memory management perspective, how to code in c to create a managed
heap
that can be extended and shrunk ?
Thanks for help in advance.
Considering the fact that doing so -- if you can at all -- is platform
specific, addressing your question to a newsgroup specific to your
platform is the right way to go. Here, you're off topic.

HTH,
--ag
 
T

Thomas Matthews

Liu said:
From memory management perspective, how to code in c to create a managed
heap
that can be extended and shrunk ?
Thanks for help in advance.

Qingzhu

First off, the C language does not require an implementation
to have a heap. There is automatic memory and dynamic memory.

If you would like to manage the dynamic memory yourself,
then use malloc() and allocate a huge chunk.

As far as shrinking or expanding a program's dynamic memory,
that is up to the operating system and best discussed in a
newsgroup devoted to your operating system.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
M

Malcolm

Liu said:
From memory management perspective, how to code in c to create a > managed
heap that can be extended and shrunk ?The basic way to implement a meory allocation scheme is to declare an arena

static BLOCK arena[100000];

You then allocate blocks which consist of

typedef struct block
{
struct block *next;
size_t size;
}BLOCK;

The free list consists of a linked list of a a series of blocks. Initially
the whole arena is one big free block. When you allocate, walk the free
list until you find a block that is large enough. Then you spilt it and
return the pointer that is immediately after the control block.
When you free, walk the free list until you find the place where the ponter
is greater than the preceding and less than the following block, then you
have to add the block to the free list and consolidate if possible.
The trick is that you always return the control block +1, so the size is
always stored immediately before the memory you allocate.

If you know the arena size at compile time, simply hard code it in. If you
have to query the operating system to know how much memory you are allowed
to use, you leave the realms of comp.lang.c, and you will have to ask in a
platform-specific group.
 
M

Mark McIntyre

Considering the fact that doing so -- if you can at all -- is platform
specific, addressing your question to a newsgroup specific to your
platform is the right way to go. Here, you're off topic.

I don't agree. You can implement one in Standard C if you want to. You're
confusing the existence or otherwise of some heap that the implementation
may use, with the creation of a user-defined heap of your own.

I do think its more of an algo question first tho. Work out the algo
(pehaps in comp.programming) , then work out how to code it in C.
 
A

Artie Gold

Mark said:
I don't agree. You can implement one in Standard C if you want to. You're
confusing the existence or otherwise of some heap that the implementation
may use, with the creation of a user-defined heap of your own.

Managing a "heap" is one thing, but "expanding or shrinking" in standard
C? I just don't see it (unless, of course, you're willing to have it
move around, too).
I do think its more of an algo question first tho. Work out the algo
(pehaps in comp.programming) , then work out how to code it in C.
Cheers,
--ag
 
R

Richard Bos

Artie Gold said:
Managing a "heap" is one thing, but "expanding or shrinking" in standard
C? I just don't see it (unless, of course, you're willing to have it
move around, too).

Well, apart from realloc(), you can in fact allocate it in chunks and
put those in a linked list, if necessary.

Richard
 
A

Artie Gold

Richard said:
Well, apart from realloc(), you can in fact allocate it in chunks and
put those in a linked list, if necessary.

Of course. <splat -- sound of palm of hand hitting forehead>.
[Oh how I hate cerebral flatulence.]

Cheers,
--ag
 
M

Mark McIntyre

Managing a "heap" is one thing, but "expanding or shrinking" in standard
C?

whats wrong with realloc?
I just don't see it (unless, of course, you're willing to have it
move around, too).

I don't have a problem with that. Did the OP say he did? I can't recall.
 
C

CBFalconer

Mark said:
Liu, Qingzhu [CAR:2N84:EXCH] wrote:
From memory management perspective, how to code in c to create
a managed heap that can be extended and shrunk ?

Considering the fact that doing so -- if you can at all -- is
platform specific, addressing your question to a newsgroup
specific to your platform is the right way to go. Here, you're
off topic.

I don't agree. You can implement one in Standard C if you want to.
You're confusing the existence or otherwise of some heap that the
implementation may use, with the creation of a user-defined heap
of your own.

True enough. My nmalloc for DJGPP is an example, where the debug
version (controlled by defines) uses an auxiliary program (the
debug exercizer) to malloc a large arena and simulate the action
of sbrk() (non-standard). Unfortunately the addresses returned
are not necessarily constant with changes in the code, so it
doesn't serve properly for regression testing.

To the OP: you can find nmalloc on my site, download section.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Flex-shrink 3
[C#] Extend main interface on child level 0
Heap Overlays? 7
Confused about quotations 5
Heap and Stack 5
Looking for programmers! 3
stack and heap 9
Need a help to create Code 0

Members online

Forum statistics

Threads
474,139
Messages
2,570,807
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top