473,762 Members | 8,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Disadvantages of malloc()

On some small-memory systems, is it true that "malloc()" is not supported by
the C library?

Why is 'dynamic memory allocation' so avoided on such systems? What are
major problems of using malloc() here?

Thanks!
Nov 14 '05 #1
11 8573
Aire wrote:
On some small-memory systems,
Is it true that "malloc()" is not supported by the C library?

Why is 'dynamic memory allocation' so avoided on such systems?
What are major problems of using malloc() here?


The comp.lang.c newsgroup
is a good place to get *bad* advice about any particular architecture.

Try the comp.arch.embed ded newsgroup instead.

Nov 14 '05 #2
In article <k8************ *********@bgtns c05-news.ops.worldn et.att.net>,
Aire <ai*****@zoomit .org> wrote:
On some small-memory systems, is it true that "malloc()" is not supported by
the C library?
All systems claiming to support a hosted C implementation are required
to support malloc.[1]

These days most small-memory systems are freestanding implementations ,
which are not required to implement most of the standard library; malloc
is a popular candidate for a part of the standard library to leave out.

Why is 'dynamic memory allocation' so avoided on such systems? What are
major problems of using malloc() here?


Usually because dynamic memory allocation isn't necessary and makes
possible some problems that are hard to solve in limited environments.

A lot of the regulars here have worked with freestanding implementations
in limited environments and will be able to tell you more than
you ever wanted to know about the details I'm waving my hands over.
comp.arch.embed ded (discussing embedded systems, where most freestanding
C implementations are used) might also be able to shed some light on
the subject.
dave

[1] Well, at least if they want people to believe the claim.

--
Dave Vandervies dj******@csclub .uwaterloo.ca
Start with your English classes; only when you've got the point that spelling
is not completely optional are you ready to proceed to the rather more exacting
discipline of programming. --Richard Bos in comp.lang.c
Nov 14 '05 #3

"Aire" <ai*****@zoomit .org> wrote in message news:

On some small-memory systems, is it true that "malloc()" is not
supported by the C library?
Yes, some not-so small systems like games consoles don't support it either.
Why is 'dynamic memory allocation' so avoided on such systems?
What are major problems of using malloc() here?

As long as you can declare a global array of reasonable size it is easy to
implement malloc() (call it mymalloc()).
There are lots of reasons for not providing malloc(), but mainly so that the
programmer has complete control over every byte allocated. For instance one
game I wrote used a lot of small allocations of the same size, so I
customised the memory allocator to provide these efficiently. using a
built-in malloc() this would not have been posible.
Nov 14 '05 #4
In <k8************ *********@bgtns c05-news.ops.worldn et.att.net> "Aire" <ai*****@zoomit .org> writes:
On some small-memory systems, is it true that "malloc()" is not supported by
the C library?
It depends on what you mean by small-memory system. A conforming hosted
C89 implementation must provide at least 32k of RAM. Otherwise it can
only be a conforming freestanding implementation and, in this case, it
need not provide a malloc() implementation.
Why is 'dynamic memory allocation' so avoided on such systems? What are
major problems of using malloc() here?


Dynamically allocated memory has certain overheads that statically and
automatically memory don't. A malloc(100) call would always use more
than 100 bytes of memory, because the malloc() implementation must keep
track at least of the block's size, but typically more data is kept about
each dynamically allocated memory block. Then, there is the effect of
malloc arena fragmentation, caused by multiple allocations and
deallocations. In a pathologic scenario, a significant part of the malloc
arena becomes unusable by the program and, therefore, wasted.

OTOH, if you can't predict the size of a memory block when writing the
code, you have to either overallocate it statically/automatically or
dynamically allocate it. The former alternative may waste even more
memory.

Another issue is what happens when your program runs out of memory.
If it's due to statically allocated memory, it will fail to load.
If it's due to automatically allocated memory, it will crash at
run time. If it's due to dynamically allocated memory, you're still
controlling the situation and have the option on choosing the best way
of proceeding.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
"Aire" <ai*****@zoomit .org> wrote in message news:<k8******* **************@ bgtnsc05-news.ops.worldn et.att.net>...
On some small-memory systems, is it true that "malloc()" is not supported by
the C library?

Why is 'dynamic memory allocation' so avoided on such systems? What are
major problems of using malloc() here?

Thanks!


Dan has covered the reasons, I will only add that a lot can be learnt
by reading malloc implementations . There are quite a few good ones on
the web, all of which demonstrate the problems.
Nov 14 '05 #6
On Tue, 27 Jan 2004 22:31:21 -0500, Rob Thorpe wrote:
Why is 'dynamic memory allocation' so avoided on such systems? What
are major problems of using malloc() here?

Thanks!


Dan has covered the reasons, I will only add that a lot can be learnt by
reading malloc implementations . There are quite a few good ones on the
web, all of which demonstrate the problems.


The stdlib malloc is also usually protected by locks for multithreaded
programs which makes it a little slower than a lockless allocator like
this one:

http://www.ioplex.com/~miallen/libmba/dl/src/suba.c

Of course if you are writing a multithreaded application you have to
be more careful. Suba is also only ~250 lines of code so it might
be a good example to look at or extend if you're exploring memory
allocation. There's nothing fancy about it -- just a circular linked
list of "cells".

Mike
Nov 14 '05 #7
> On some small-memory systems, is it true that "malloc()" is not supported by
the C library?

Why is 'dynamic memory allocation' so avoided on such systems? What are
major problems of using malloc() here?


It means you run the risk of running out of memory, not to
mention memory fragmentation.

Suppose you have a system with 64K of memory, and your application
does some dynamic allocation. It all works fine, until other
applications are loaded onto the system, and then one day when they
all happen to have malloc'd at once , the system crashes and everybody
blames everyone else's application. This is doubly important if the
crashed happened at an important time (eg. during open heart surgery).

It's far more reliable to allocate all memory at compile-time
(ie. a fixed-size stack, and no malloc calls). You can inspect
your code to ensure that the stack will never overflow.

I have heard of systems that support malloc() (just to be conforming)
but always fail (return NULL). Others just don't bother
with the rigmarole and don't support it.
Nov 14 '05 #8

"Old Wolf" <ol*****@inspir e.net.nz> wrote in message
news:84******** *************** ***@posting.goo gle.com...
On some small-memory systems, is it true that "malloc()" is not supported by
the C library?

Why is 'dynamic memory allocation' so avoided on such systems? What are
major problems of using malloc() here?
It means you run the risk of running out of memory, not to
mention memory fragmentation.

Suppose you have a system with 64K of memory, and your application
does some dynamic allocation. It all works fine, until other
applications are loaded onto the system, and then one day when they
all happen to have malloc'd at once , the system crashes and everybody
blames everyone else's application. This is doubly important if the
crashed happened at an important time (eg. during open heart surgery).


Open heart surgery with a machine that has 64K memory?

On the reality side, most modern operating systems
today use virtual memory and isolate each application
(i.e., "process") within its own virtual space. E.g.,
address 0x1000 translates to a different piece of real
memory for each application. The system crashes only
when it runs out of paging space on the harddrive. Then
it's a problem of simply running too many processes at
the same time. It's not the fault of the application,
but the users that loaded too many applications on
the same box at the same time.
It's far more reliable to allocate all memory at compile-time
(ie. a fixed-size stack, and no malloc calls). You can inspect
your code to ensure that the stack will never overflow.
That only moves the fragmentation from malloc()
to stackframe management. Stackframe memory and
malloc() memory come from the same place. The
stack frame is fixed-size, but the compiler cannot
determine how much memory is needed for all stack
frames (recursive calls, function pointer calls).

Most real world problems are solved much more
efficiently with malloc() memory than swagging
a giant array on the stackframe. Load up too
many of those kinds of programs and you have
the same problems as too many malloc()-style
programs running at the same time.
I have heard of systems that support malloc() (just to be conforming)
but always fail (return NULL). Others just don't bother
with the rigmarole and don't support it.


Design your C programs to use stack frame
memory and heap memory using prudent policies,
rather than fearing that someday your program
will be ported to a Deathstation200 0. Stay
grounded in the real world.
--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
Are ISV upgrade fees too high? Check our custom product development!
Nov 14 '05 #9
> > It means you run the risk of running out of memory, not to
mention memory fragmentation.

Suppose you have a system with 64K of memory, and your application
does some dynamic allocation. It all works fine, until other
applications are loaded onto the system, and then one day when they
all happen to have malloc'd at once , the system crashes and everybody
blames everyone else's application. This is doubly important if the
crashed happened at an important time (eg. during open heart surgery).
Open heart surgery with a machine that has 64K memory?


A machine that, say, measures out dosages of medicines, would not
need much memory. Anyway I'm sure you can think of lots
of better examples..
On the reality side, most modern operating systems
today use virtual memory and isolate each application
(i.e., "process") within its own virtual space. E.g.,
address 0x1000 translates to a different piece of real
memory for each application. The system crashes only
when it runs out of paging space on the harddrive. Then
it's a problem of simply running too many processes at
the same time. It's not the fault of the application,
but the users that loaded too many applications on
the same box at the same time.
I program on financial transaction processing devices,
most of which have no memory protection and no hard drive
(they have static ram). You are lucky if you get a
stack and 32-bit pointers :)
It's far more reliable to allocate all memory at compile-time
(ie. a fixed-size stack, and no malloc calls). You can inspect
your code to ensure that the stack will never overflow.


That only moves the fragmentation from malloc()
to stackframe management. Stackframe memory and


Stack memory does not get fragmented.
malloc() memory come from the same place. The
stack frame is fixed-size, but the compiler cannot
determine how much memory is needed for all stack
frames (recursive calls, function pointer calls).
Which is why you avoid recursion and function pointers
(unless you are being exceptionally careful). It's possible
to plot (as a human, I mean) all possible function call
paths and make sure you have a lot more stack than the
longest path will take.
Most real world problems are solved much more
efficiently with malloc() memory than swagging
a giant array on the stackframe. Load up too
many of those kinds of programs and you have
the same problems as too many malloc()-style
programs running at the same time.


Not at all; they each get their own fixed-size
stack allocated at compile-time and cannot
interfere with each other's memory allocation.
Nov 14 '05 #10

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

Similar topics

8
23180
by: Wilbur | last post by:
My company is considering using C# for a large project and it is a language I'm only just now becoming familiar with. For the most part I like it and that seems to be the most logical choice as far as future support. My question is: what are the disadvantages or limitations of using C#? So far I've seen very few people willing to mention anything "bad" about it, but every language has it's faults. We would be using C# in the .NET Framework...
3
41537
by: enchantingdb | last post by:
I have an exam tomorrow that covers the perceived advantages and disadvantages of object oriented programming, in particular polymorphism, inheritance and encapsulation. I know the advantages but am not clear on the disadvantages. I have had a look on the Web and in newsgroups but couldn't find much. As time is running out, I thought I would post here and hope that someone would reply. Thanks Rob
21
11216
by: EmJayEm | last post by:
Can someone tell me the Disadvantages/Cons of web services? Thanks, EmJ.
54
6455
by: m.roello | last post by:
In the book: "Working with Microsoft Visual Studio 2005" Craig Skibo wrote: "The power of Visual Studio 2005 lies in its ability to empower users to build, test, and debug powerful applications quickly and easly." I don't agree on what concernes ASP .NET Web Sites in VS2005. All what involves Namespaces in Web sites has been disappeared. I know you can still MANUALLY manage them, but not QUICKLY and EASLY. In a
1
3941
by: Husam | last post by:
Hi EveryBody: I have research about advantages and disadvantages of C++ languages In one of my programming courses. Can Any one help me and told me about advantages and disadvantages of C++ or redirect me to some web sites proffessinal about this kind of reserach ? any help will be appreciated
11
7335
by: GVN | last post by:
Hi All, Can anyone guide me when asynchronous method calls will be benificial? Are there any disadvantages of using asynchronous calls? Thanks,
1
9171
by: vumani | last post by:
what is the advantages and disadvantages of Ms SQL server and java servletts front-end on the clien end. what is the advantages and disadvantages of Ms Access on the server, connected via JDBC and java on the client end. what is the advantages and disadvantages of HTML on the client end,coupled with SQL server and ODBL on the server end.
0
10137
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...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9927
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
9812
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
8814
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
6640
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
5268
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.