Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to allocate mem without using malloc() & free without using free()

Reply
Thread Tools

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

 
 
Rajshekhar
Guest
Posts: n/a
 
      03-24-2005
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
 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      03-24-2005
Rajshekhar wrote:
> 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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      03-24-2005
Rajshekhar wrote:
>
> 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/>

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-24-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) (Rajshekhar) writes:
> 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()?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      03-24-2005
In article <(E-Mail Removed) >,
Rajshekhar <(E-Mail Removed)> wrote:
: 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?
--
Any sufficiently old bug becomes a feature.
 
Reply With Quote
 
Jonathan Bartlett
Guest
Posts: n/a
 
      03-29-2005
Rajshekhar wrote:
> Hi All,
> any pointers or alternate implementations to allocate memory without
> using std function malloc() & free memory without using free() ...!
>


See my article at:

http://www.ibm.com/developerworks/li...rary/l-memory/

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
init of class members : mem(0) vs. mem() vs. not-init at all news.aon.at C++ 11 01-29-2011 07:30 PM
allocating mem in a function and assigning a ptr to the first byte of that mem array... mast2as@yahoo.com C Programming 8 03-01-2007 08:18 AM
How OS tackles mem. freed by free method apoorv C Programming 3 02-23-2005 04:36 PM
should I free mem in function after alloc and returning pointer... ? beetle C Programming 25 01-31-2005 11:22 AM



Advertisments