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

D

dsptechie

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.
 
G

Gordon Burditt

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?
I came to know that
malloc() allocates memory only in powers of 2.

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.
i.e if asked for say 17
bytes , in the process, it allocates 32 bytes and returns 17 bytes so
15 bytes are wasted...

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.
Is this true...If anybody knows the algorithm details of malloc()
plase share the same.

C does not specify algorithm details. You failed to identify
what platform you are interested in, so nobody can answer the
question.
Is this one of the reason why malloc() is not generally preferred in
embedded systems..?

No, because, particularly on embedded systems, it would NOT be implemented
this way.


Gordon L. Burditt
 
N

Neil Kurzman

dsptechie said:
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.

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.
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top