Dmalloc Logo


Search this site:

If dmalloc has saved you or your company time or money, please use your credit-card or PayPal account to donate to the cause.

Dmalloc Tutorial: 2.3 Basic Description of Terms and Functions
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.3 Basic Description of Terms and Functions

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.3.1 General Memory Terms and Concepts

Any program can be divided into 2 logical parts: text and data. Text is the actual program code in machine-readable format and data is the information that the text operates on when it is executing. The data, in turn, can be divided into 3 logical parts according to where it is stored: static, stack, and heap.

Static data is the information whose storage space is compiled into the program.

/* global variables are allocated as static data */
int numbers[10];


Stack data is data allocated at runtime to hold information used inside of functions. This data is managed by the system in the space called stack space.

void foo()
        /* this local variable is stored on the stack */
        float total;


Heap data is also allocated at runtime and provides a programmer with dynamic memory capabilities.

        /* the address is stored on the stack */
        char * string;

         * Allocate a string of 10 bytes on the heap.  Store the
         * address in string which is on the stack.
        string = (char *)malloc(10);

        /* de-allocate the heap memory now that we're done with it */

It is the heap data that is managed by this library.

Although the above is an example of how to use the malloc and free commands, it is not a good example of why using the heap for runtime storage is useful.

Consider this: You write a program that reads a file into memory, processes it, and displays results. You would like to handle files with arbitrary size (from 10 bytes to 1.2 megabytes and more). One problem, however, is that the entire file must be in memory at one time to do the calculations. You don't want to have to allocate 1.2 megabytes when you might only be reading in a 10 byte file because it is wasteful of system resources. Also, you are worried that your program might have to handle files of more than 1.2 megabytes.

A solution: first checkout the file's size and then, using the heap-allocation routines, get enough storage to read the entire file into memory. The program will only be using the system resources necessary for the job and you will be guaranteed that your program can handle any sized file.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.3.2 Functionality Supported by All Malloc Libraries

All malloc libraries support 4 basic memory allocation commands. These include malloc, calloc, realloc, and free. For more information about their capabilities, check your system's manual pages - in unix, do a man 3 malloc.


void *malloc ( unsigned int size )

Usage: pnt = (type *)malloc(size)

The malloc routine is the basic memory allocation routine. It allocates an area of size bytes. It will return a pointer to the space requested.


void *calloc ( unsigned int number, unsigned int size )

Usage: pnt = (type *)calloc(number, size)

The calloc routine allocates a certain number of items, each of size bytes, and returns a pointer to the space. It is appropriate to pass in a sizeof(type) value as the size argument.

Also, calloc nulls the space that it returns, assuring that the memory is all zeros.


void *realloc ( void *old_pnt, unsigned int new_size )

Usage: new_pnt = (type *)realloc(old_pnt, new_size)

The realloc function expands or shrinks the memory allocation in old_pnt to new_size number of bytes. Realloc copies as much of the information from old_pnt as it can into the new_pnt space it returns, up to new_size bytes. If there is a problem allocating this memory, 0L will be returned.

If the old_pnt is 0L then realloc will do the equivalent of a malloc(new_size). If new_size is 0 and old_pnt is not 0L, then it will do the equivalent of free(old_pnt) and will return 0L.


void free ( void *pnt )

Usage: free(pnt)

The free routine releases allocation in pnt which was returned by malloc, calloc, or realloc back to the heap. This allows other parts of the program to re-use memory that is not needed anymore. It guarantees that the process does not grow too big and swallow a large portion of the system resources.

WARNING: there is a quite common myth that all of the space that is returned by malloc libraries has already been cleared. Only the calloc routine will zero the memory space it returns.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by Gray Watson on May, 16 2007 using texi2html 1.76.

This work is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.
This page should be W3C Valid XHTML and should work with most browsers.