Does malloc() allocate memory only in powers of 2...?

Discussion in 'C Programming' started by dsptechie, Aug 9, 2004.

  1. dsptechie

    dsptechie Guest

    I wanted to how exactly malloc() function works. I came to know that
    malloc() allocates memory only in powers of 2. i.e if asked for say 17
    bytes , in the process, it allocates 32 bytes and returns 17 bytes so
    15 bytes are wasted...

    Is this true...If anybody knows the algorithm details of malloc()
    plase share the same.

    Is this one of the reason why malloc() is not generally preferred in
    embedded systems..?

    Thanks in advance.
    dsptechie, Aug 9, 2004
    1. Advertisements

  2. dsptechie wrote on 09/08/04 :
    It depends on the implementation.
    Emmanuel Delahaye, Aug 9, 2004
    1. Advertisements

  3. I wanted to how exactly malloc() function works.
    How are you going to get hold of every platform it is implemented on?
    And where will you store all of them?
    On your implementation, perhaps (and I don't think this is that
    uncommon). I know there are many where it does not. Unless you
    did further investigation, it is also possible that it allocates
    only in multiples of 32, NOT powers of two.
    You might check what it does if you ask for 30 bytes, or 32 bytes.
    There is often overhead in addition to the next-power-of-two
    if that's what it's using.
    C does not specify algorithm details. You failed to identify
    what platform you are interested in, so nobody can answer the
    No, because, particularly on embedded systems, it would NOT be implemented
    this way.

    Gordon L. Burditt
    Gordon Burditt, Aug 9, 2004
  4. dsptechie

    Neil Kurzman Guest

    There are several reasons not to use malloc in embedded systems.
    1. Overhead. memory is not plentiful in small systems. bytes are wasted
    in the allocation process.
    2. Fragmentation. if maloc fail the system fails.
    3. Usually only 1 app is running. So, just take all the memory you do not
    have to share.
    4. You know how big everything is up front. You better know or you can
    not be sure if it will work in all cases.
    Neil Kurzman, Aug 10, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.