Posted on 20th October 2009No Responses
What is C’s Dynamic Allocation Functions

C’s Dynamic Allocation Functions
Pointers provide necessary support for C/C++’s dynamic allocation system. Dynamic allocation is the means by which a program can obtain memory while it is running. As you know, global variables are allocated storage at compile time. Local variables use the stack. However, neither global variable nor local variables can be added during program execution. Yet there will be times when the storage needs of a program cannot be known ahead of time. For example, a word processor or a database should take advantage of all the RAM in a system. However, because the amount of available RAM varies between computers, such programs will not be able to do so using normal variables. Instead, these and other programs must allocate memory as they need it.
C++ actually supports two complete dynamic allocation systems: the one defined by C and the one specific to C++. The system specific to C++ contains several improvements over that used by C, and this approach is discussed in Part Two. Here, C’s dynamic allocation functions are described.
Memory allocated by C’s dynamic allocation functions is obtained from the heap— the region of free memory that lies between your program and its permanent storage area and the stack. Although the size of the heap is unknown, it generally contains a fairly large amount of free memory.
The core of C’s allocation system consists of the functions malloc() and free(). (Most compilers supply several other dynamic allocation functions, but these two are the most important.) These functions work together using the free memory region to establish and maintain a list of available storage. The malloc() function allocates memory and the free() function releases it. That is, each time a malloc() memory request is made, a portion of the remaining free memory is allocated. Each time a free() memory release call is made, memory is returned to the system. Any program that uses these functions should include the header file stdlib.h. (A C++ program may also use the new-style header ). The malloc() function has this prototype:
void *malloc(size_t number_of_bytes);
Here, number_of_bytes is the number of bytes of memory you wish to allocate. (The type size_t is defined in stdlib.h as, more or less, an unsigned integer). The malloc() function returns a pointer of type void, which means that you can assign it to any type of pointer. After a successful call, malloc() returns a pointer to the first byte of the region of memory allocated from the heap. If there is not enough vailable memory to satisfy the malloc() request, an allocation failure occurs and malloc() returns a null. The code fragment shown here allocates 1,000 bytes of contiguous memory:
char *p;
p = malloc(1000); /* get 1000 bytes */

After the assignment, p points to the start of 1,000 bytes of free memory. In the preceding example, notice that no type cast is used to assign the return value of malloc() to p. In C, a void *pointer is automatically converted to the type of the pointer on the left side of an assignment. However, it is important to understand that this automatic conversion does not occur in C++. In C++, an explicit type cast is needed when a void *pointer is assigned to another type of pointer. Thus, in C++, the preceding assignment must be written like this:
p = (char *) malloc(1000)
As a general rule, in C++ you must use a type cast when assigning (or otherwise converting) one type of pointer to another. This is one of the few fundamental differences between C and C++.
The next example allocates space for 50 integers. Notice the use of sizeof to ensure portability.
int *p;
p = (int *) malloc(50*sizeof(int));

Since the heap is not infinite, whenever you allocate memory, you must check the value returned by malloc() to make sure that it is not null before using the pointer. Using a null pointer will almost certainly crash your program. The proper way to allocate memory and test for a valid pointer is illustrated in this code fragment:

p = (int *) malloc(100);
if(!p) {
printf(”Out of memory.\n”);
exit(1);
}

Of course, you can substitute some other sort of error handler in place of the call to exit(). Just make sure that you do not use the pointer p if it is null.
The free() function is the opposite of malloc() in that it returns previously allocated memory to the system. Once the memory has been freed, it may be reused by a subsequent call to malloc(). The function free() has this prototype:
void free(void *p);
Here, p is a pointer to memory that was previously allocated using malloc(). It is critical that you never call free() with an invalid argument; this will destroy the free list.

Popularity: 1% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • BlinkList
  • blogmarks
  • BlogMemes Fr
  • BlogMemes Sp
  • Blogosphere News
  • blogtercimlap
  • co.mments
  • connotea
  • Current
  • Design Float
  • Diigo
  • DotNetKicks
  • DZone
  • eKudos
  • email
Comments
Leave a Response