473,804 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.
Nov 14 '05 #1
3 5222
dsptechie wrote on 09/08/04 :
I wanted to how exactly malloc() function works.


It depends on the implementation.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #2
>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
Nov 14 '05 #3


dsptechie wrote:
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.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4770
by: Hongzheng Wang | last post by:
Hi, everyone I have a problem about malloc/free function. Does malloc add size information to program? And, when free function is called, how this function get the size information? That is, if I request a memory block of size 10, where the size information 10 is stored? To be clear, I have such codes below: int *p = (int *) malloc(10*sizeof(int)); /* ...... */
24
3833
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
2
1432
by: dsptechie | last post by:
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..?
74
4066
by: Suyog_Linux | last post by:
I wish to know how the free()function knows how much memory to be freed as we only give pointer to allocated memory as an argument to free(). Does system use an internal variable to store allocated memory when we use malloc(). Plz help......
8
1793
by: lasek | last post by:
Hi...i'm writing from Rome...and i don't know english very well so... Only simple question, which is the difference between those two parts of code. int *pInt=NULL; int iVar=10; pInt=&iVar;
6
2319
by: lovecreatesbeauty | last post by:
Hello experts, 1. Does C guarantee the data layout of the memory allocated by malloc function on the heap. I mean, for example, if I allocate a array of 100 elements of structure, can I always reference a correct/valid structure member upon that allocated memory? If I allocate memory, for example like this way:
49
23201
by: Karsten Jung | last post by:
Hello together, We have to make our own implementation of malloc() under Linux. Does anybody can give me a hint where to start? Our implementation should only work under Linux. Thanks Karsten
5
8096
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space assigned to <pointerby "new". Does "new" create a list of pointer names and the size of the memory array they point to? I also read that some compilers may store the number of consec mem locations a pointer points to, just before the first data...
25
3391
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. Inside each structure, I have two members: a list of strings, and a string. I have made a sample program below that exhibits the error I am having. I also read about Valgrind, and used it to tell me where I was getting the segmentation fault,...
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.