How to allocate mem without using malloc() & free without using free()

R

Rajshekhar

Hi All,
any pointers or alternate implementations to allocate memory without
using std function malloc() & free memory without using free() ...!

the functions should behave exactly like the STD functions are..

cheers ;-)
Rajshekhar
 
M

Michael Mair

Rajshekhar said:
Hi All,
any pointers or alternate implementations to allocate memory without
using std function malloc() & free memory without using free() ...!

the functions should behave exactly like the STD functions are..

#define MY_MALLOC(size) realloc(NULL, size)
#define MY_FREE(ptr) (void) realloc(ptr, 0)

Apart from that: The only way to obtain dynamic storage in a
standard conforming way such that all alignment requirements
are fulfilled _is_ to use malloc/calloc/realloc,
the only way to get rid of it _is_ to use free/realloc.
Everything else is specific for your implementation.


Cheers
Michael
 
C

CBFalconer

Rajshekhar said:
any pointers or alternate implementations to allocate memory without
using std function malloc() & free memory without using free() ...!

the functions should behave exactly like the STD functions are..

You can use the debug version of nmalloc (controlled by compile
time defines) and a fakesbrk routine. That gives you the routines
nmalloc, nfree, nrealloc. See:

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

Keith Thompson

any pointers or alternate implementations to allocate memory without
using std function malloc() & free memory without using free() ...!

the functions should behave exactly like the STD functions are..

We see a lot of questions here of the form "How do I do X without
using the standard language feature that's specifically designed to do
X?" Usually the best answer is just to use the standard language
feature.

Without knowing *why* you don't want to use malloc() and free(), I
don't think we can give you a meaningful answer. Michael Mair gave
you a solution using realloc(). That probably doesn't meet your
requirements, but we can't tell without knowing what your requirements
really are.

Why don't you want to use malloc() and free()?
 
W

Walter Roberson

: any pointers or alternate implementations to allocate memory without
:using std function malloc() & free memory without using free() ...!

malloc() and kin are there to hide the system dependancies that
you would otherwise have to use.

On a Unix system, the traditional interface to allocating or
deallocating process memory is through the brk() and sbrk() functions.
Those are, though, not part of standard C, and the procedure
for Windows 2000 might be completely different (and for Windows 95
completely different yet.)

Even within Unix, a variety of strategies are used these days, not just
brk() and sbrk(). For example, there are versions of malloc() that work
by requesting that a private shared memory segment be mapped into the
address space -- leading to discontinuous sets of valid virtual
addresses, but also making it easier to return memory to the OS.

:the functions should behave exactly like the STD functions are..

Bug for bug compatible? For example, if you overwrite the location
-before- an allocated area, and then free() the allocated area, do
you require that the implimentation trash a semi-random part of your
address space... or would it be acceptable if the implimentation put
a "guard zone" there and raised a SIGMEM when you attempted to write to
the unallocated location?
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top