about malloc, realloc and free

M

Momo

i m just wondering is there any replacement of using malloc, realloc
and free. Like will i able to make my our version of malloc realloc
and free, if so, any idea how to do it??
any help will be good
 
E

Eric Sosman

Momo said:
i m just wondering is there any replacement of using malloc, realloc
and free. Like will i able to make my our version of malloc realloc
and free, if so, any idea how to do it??
any help will be good

There are at least two answers.

First, the Standard answer: No, an implementation of C
is indivisible, and you cannot safely substitute your own
implementations for any of its functions. Some systems
incorporate the entire library into every program as a unit,
and have no way to omit pieces of it to avoid clashes with
functions you've defined. Others are capable of omitting
pieces, but have non-public "back doors" for the library's
internal use; if you provide a memory management package
that meets all the "public" specifications but fails to
provide the implementation-private interfaces, you can get
mysterious breakage in unexpected places.

Second, the practical answer: Many systems *do* permit
various pieces of the C library to be replaced by user-
written functions, and the memory management functions may
well be the most frequently replaced (followed by the math
functions, I'd guess). Common reasons for replacement are
to introduce debugging features (watch for leaks, double-
frees, area overruns, and so on) or for improved efficiency
(when the design point of a "general purpose" implementation
isn't a good match for the needs of the program). It's like
driving faster than the speed limit: illegal, but Everybody
Does It -- and if you use care and enjoy a certain amount of
luck, you'll get to your destination sooner.
 
R

Richard Bos

i m just wondering is there any replacement of using malloc, realloc
and free.

Why? What's wrong with the existing functions?
Like will i able to make my our version of malloc realloc
and free, if so, any idea how to do it??

Yes and no.
First, under ISO C it's undefined behaviour to define a function with
the same name as a Standard function, so you can't call your function
malloc(). The usual solution to this problem is to call it something
else :), for example xmalloc().
Second, it's not likely that your function is going to be more efficient
than the implementation's version, and if you stick to ISO C (i.e., no
brk() and so forth), it's likely to be considerably less powerful.

Richard
 
C

CBFalconer

Momo said:
i m just wondering is there any replacement of using malloc, realloc
and free. Like will i able to make my our version of malloc realloc
and free, if so, any idea how to do it??

In the user world, no - it is expressly forbidded by the standard.
In the implementors world, yes. For an example see nmalloc for
DJGPP at:

<http://cbfalconer.home.att.net/download/>
 
L

Lawrence Kirby

i m just wondering is there any replacement of using malloc, realloc
and free. Like will i able to make my our version of malloc realloc
and free, if so, any idea how to do it??
any help will be good

If you replace malloc and friends the obvious question is where you would
get the memory to be allocated from, C provides no other functions for
this. At best you could use some static array of characters, or else you
would have to use some system-specific extension. As others have said you
cannot safely replace the malloc etc. functions in the standard library.

One thing you can do and which usefully done in some circumstances is
define your own wrapper functions with different names. These can use
malloc etc. to get their memory and manage it their own way. These can be
advantages e.g. when allocating quantities of fixed length objects.

Lawrence
 
S

SM Ryan

(e-mail address removed) (Momo) wrote:
# i m just wondering is there any replacement of using malloc, realloc
# and free. Like will i able to make my our version of malloc realloc
# and free, if so, any idea how to do it??
# any help will be good

There are many replacements, including one with garbage collection. You'll need
to find out how to load the alternatives on your specific system. Alternatively used
sometimes is to put your wrappers around the usual calls. In one case I mallocked
space in each block for where it was allocated or reallocated, and then returned
the remaining space from my allocator. I just had to remember whether to use the
library free or my deallocator.
 
C

CBFalconer

Richard said:
(e-mail address removed) (Momo) wrote:
.... snip ...

.... snip ...
and if you stick to ISO C (i.e., no brk() and so forth), it's
likely to be considerably less powerful.

In fact it is impossible to do in strictly conforming ISO C, which
is the fundamental reason that those routines are provided.
 
M

Malcolm

Momo said:
i m just wondering is there any replacement of using malloc, realloc
and free. Like will i able to make my our version of malloc realloc
and free, if so, any idea how to do it??
any help will be good
There are many libraries avialable, and lots of approaches you can use.

A simple implementation of malloc uses a large static global array.

You have a control block which consists of a size member, a marker for
allocated/ freed, and pointers to the next and previous blocks. The trick is
that the pointer returned by malloc is just past the control block, so
free() can retrieve the control information by subtracting one (block) from
the pointer passed to it. The hard part is consolidating blocks in the free
list.

There are a few problems with the standard. You can get into trouble if you
use the identifier malloc() - not all compiler will allow you to provide
your own version of a standard library function. The other problem is that
data must be aligned. In practise this can be achieved by putting in a dummy
"double" member in a union with your control block, and always allocating
whole blocks. However it is not guaranteed that double is the most strictly
aligned type.
 
K

Keith Thompson

Malcolm said:
There are a few problems with the standard. You can get into trouble if you
use the identifier malloc() - not all compiler will allow you to provide
your own version of a standard library function. The other problem is that
data must be aligned. In practise this can be achieved by putting in a dummy
"double" member in a union with your control block, and always allocating
whole blocks. However it is not guaranteed that double is the most strictly
aligned type.

In fact, it's likely that long double is more strictly aligned than
double. (The reverse is also possible.)

There's no guaranteed way to force safe alignment in portable code,
but you can come close with a union, something like:

union align {
int i;
long int l;
long long int ll; /* assuming C99 or extended C90 */
float f;
double d;
long double ld;
/* etc. */
}

where the "/* etc. */" should include pointers to structs and
functions, and perhaps one or more complex types.
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top