Design Article
Dynamic allocation in C and C++
Dan Saks
6/30/2008 6:00 AM EDT
I recently presented arguments for and against using dynamic memory allocation in C and C++ programs.1 I do agree that truly safety-critical systems should avoid using dynamic allocation because the associated risks outweigh the advantages. However, I strongly suspect that many other embedded systems could be improved by judiciously using dynamic memory allocation. In places where the standard C or C++ allocation and deallocation functions are less than ideal, a customized memory manager might work much better.
Ideally, a customized memory manager should look and act like a standard one to the greatest extent possible. Although the customized allocation policy will be different from the policy of a standard one (that's the whole point of using a custom scheme), the parameters and return values of the customized allocation and deallocation functions should be as similar as possible to the standard ones. This similarity lets you build and test your code using standard memory management functions and then slip customized functions in or out as needed with minimal fuss.
Understanding the ins and outs of dynamic memory management is especially worthwhile for C++ programmers. C++ offers many facilities--most notably classes with constructors and destructors--that dramatically diminish the incidence of memory leaks. C++ also lets you define allocation and deallocation functions for each class, making it remarkably easy to insert customized memory managers into existing code. You can even use allocation functions to place objects representing device registers at memory-mapped locations.
This month, I'll contrast the standard allocation and deallocation facilities in C with those in C++. Understanding the differences between these facilities is insightful regardless of which language you use.
The standard C functions
Standard C provides two memory allocation functions, malloc and calloc, and one deallocation function, free. A fourth function, realloc, does both deallocation and allocation. All four functions are declared in the standard header <stdlib.h>.




gheorghe
7/17/2008 5:36 PM EDT
A car maker no longer discusses about horses.
When a "software maker" will not discuss aboutC and C++? My response is:You think about tomorrow as today. An invention changes all, you know! You think about transistor!!!!
I'm working to a fundamental invention in software: M# The Universal Programming Language of the Informational Space based on a new informational entity called The Informational Individual.
This is the most important invention of the modern society!!
My project is a complete applicative software system for people, companies or organizations of any kind (industry, banks, health, office, government, defense, â¦) and any dimensions (very small, small, middle, big or very big).
My Project means:
1. A very power alternative vision to the current software model.
3. A new conceptual approach in software, on a new model â inspired from the real world and having the same stability as the real world.
4. A new kind of thinking in software, a new context of work, a new perspective, from now forever.
5. A network of computers. In the future we will work on a network of Informational Individuals â InfoIndNet!
6. The end of the world software supremacy for a country or a group of companies!
7. IMPACT. Future. Technology.
The Informational Individual opens a new universe! This is limited only of our thinking, of our imagination, of our intuition. There is no limit!
The Informational Individual is the universal informational seed for what we want to do!!
The Informational Individual is built as a collaborative entity by definition. For any informational needs, someone can build an Informational Individual. The network of the Informational Individuals can have any type of Expert Individual you want.
The Informational Individual is the final frontier of the software concepts.
Sign in to Reply
prashant_sathe
7/23/2008 6:46 AM EDT
Sir,
I read your reply to the "Dynamic allocation in Cand C++" of Dan Saks
I like your concept behind this.
I would love to work with you if you are doing the above.
pls reply me on my comapany id sprashant@printelectronics.com
thank you
regards,
prashant sathe
/***************************************************************************************************************************************************************/
>>gheorghe
>>I work to a fundamental invention in so
>>commented on Jul 17, 2008 5:36:56 PM
A car maker no longer discusses about horses.
When a "software maker" will not discuss aboutC and C++? My response is:You think about tomorrow as today. An invention changes all, you know! You think about transistor!!!!
I'm working to a fundamental invention in software: M# The Universal Programming Language of the Informational Space based on a new informational entity called The Informational Individual.
This is the most important invention of the modern society!!
My project is a complete applicative software system for people, companies or organizations of any kind (industry, banks, health, office, government, defense, â¦) and any dimensions (very small, small, middle, big or very big).
My Project means:
1. A very power alternative vision to the current software model.
3. A new conceptual approach in software, on a new model â inspired from the real world and having the same stability as the real world.
4. A new kind of thinking in software, a new context of work, a new perspective, from now forever.
5. A network of computers. In the future we will work on a network of Informational Individuals â InfoIndNet!
6. The end of the world software supremacy for a country or a group of companies!
7. IMPACT. Future. Technology.
The Informational Individual opens a new universe! This is limited only of our thinking, of our imagination, of our intuition. There is no limit!
The Informational Individual is the universal informational seed for what we want to do!!
The Informational Individual is built as a collaborative entity by definition. For any informational needs, someone can build an Informational Individual. The network of the Informational Individuals can have any type of Expert Individual you want.
The Informational Individual is the final frontier of the software concepts.
Sign in to Reply
Michael.Lee
8/4/2008 5:05 AM EDT
Thanks for the author article, that's very good, moreover, we can find the relative content about dynamic allocation in C/C++ language in some excelent books, such as <>¡¢<> and so on.
Meanwhile, I also think that some one who want to be an excellent C/C++ programmer should master the content of this article first.
Sign in to Reply
rajesh284
12/18/2011 12:28 PM EST
Dynamic memory allocation is discussed here
http://clinuxpro.com/static-and-dynamic-memory-allocation
Sign in to Reply