C প্রোগ্রামিং ও Memory Allocation

Submitted by administrator on Mon, 01/02/2012 - 16:14

So far you've learned how to declare and reserve a piece of memory space before it is used in your program. For instance, you have to specify the size of an array in your program (or the compiler has to figure out the size if you declare an unsized array) before you assign any data to it at runtime. In this lesson you'll learn to allocate memory space dynamically when your program is running. The four dynamic memory allocation functions covered in this lesson are

    The malloc() function
    The calloc() function
    The realloc() function
    The free() function




Summary

  •     In C, there are four functions that can be used to allocate, reallocate, or release a block of memory space dynamically at runtime.
  •     The malloc() function allocates a block of memory whose size is specified by the argument passed to the function.
  •     The free() function is used to free up a block of memory space previously allocated by the malloc(), calloc(), or realloc() function
  •     The calloc() function can do the same job as the malloc() function. In addition, the calloc() function can initialize the allocated memory space to 0.
  •     The realloc() function is used to reallocate a block of memory that has been allocated by the malloc() or calloc() function.
  •     If a null pointer is passed to the realloc() function as its first argument, the function acts like the malloc() function.
  •     If the second argument of the realloc() function is set to 0, the realloc() function is equivalent to the free() function that releases a block of allocated memory.
  •     You have to include the header file stdlib.h before you can call the malloc(), calloc(), realloc(), or free() function.
  •     You should always check the values returned from the malloc(), calloc(), or realloc() function, before you use the allocated memory made by these functions.

 

Related Items

The #define and #undef Directives

The #define and #undef Directives

The #define directive is the most common preprocessor directive, which tells the preprocessor to replace every occurrence of a particular character string (that is, a macro name) with a specified value (that is, a macro body).

The C Preprocessor Versus the Compiler

The C Preprocessor Versus the Compiler

One important thing you need to remember is that the C preprocessor is not part of the C compiler.

What Is the C Preprocessor?

If there is a constant appearing in several places in your program, it's a good idea to associate a symbolic name to the constant, and then use the symbolic name to replace the constant throughout the program. There are two advantages to doing so. First, your program will be more readable.

Exercises : Answer the following Question

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.

Question and Answer

    Q Why is random access to a disk file necessary?