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

Discussion in 'C Programming' started by Rajshekhar, Mar 24, 2005.

  1. Rajshekhar

    Rajshekhar Guest

    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
     
    Rajshekhar, Mar 24, 2005
    #1
    1. Advertising

  2. Rajshekhar

    Michael Mair Guest

    Re: How to allocate mem without using malloc() & free without usingfree()

    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.
     
    Michael Mair, Mar 24, 2005
    #2
    1. Advertising

  3. Rajshekhar

    CBFalconer Guest

    Re: How to allocate mem without using malloc() & free without usingfree()

    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
     
    CBFalconer, Mar 24, 2005
    #3
  4. Re: How to allocate mem without using malloc() & free without usingfree()

    (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) <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.
     
    Keith Thompson, Mar 24, 2005
    #4
  5. In article <>,
    Rajshekhar <> 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.
     
    Walter Roberson, Mar 24, 2005
    #5
  6. Jonathan Bartlett, Mar 29, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dsptechie
    Replies:
    3
    Views:
    543
    Neil Kurzman
    Aug 10, 2004
  2. Replies:
    8
    Views:
    338
    jaysome
    Mar 1, 2007
  3. shsingh
    Replies:
    13
    Views:
    769
  4. SAUHING LEE

    dynamically allocate array size using malloc

    SAUHING LEE, Dec 5, 2008, in forum: C Programming
    Replies:
    7
    Views:
    3,331
  5. news.aon.at
    Replies:
    11
    Views:
    676
    Ian Collins
    Jan 29, 2011
Loading...

Share This Page