malloc, calloc - Storage Management - in C

By: Sam Chen Emailed: 1768 times Printed: 2516 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The functions malloc and calloc obtain blocks of memory dynamically.
   void *malloc(size_t n)
returns a pointer to n bytes of uninitialized storage, or NULL if the request cannot be satisfied.
   void *calloc(size_t n, size_t size)
returns a pointer to enough free space for an array of n objects of the specified size, or NULL if the request cannot be satisfied. The storage is initialized to zero.

The pointer returned by malloc or calloc has the proper alignment for the object in question, but it must be cast into the appropriate type, as in

   int *ip;

   ip = (int *) calloc(n, sizeof(int));
free(p) frees the space pointed to by p, where p was originally obtained by a call to malloc or calloc. There are no restrictions on the order in which space is freed, but it is a ghastly error to free something not obtained by calling malloc or calloc.

It is also an error to use something after it has been freed. A typical but incorrect piece of code is this loop that frees items from a list:

   for (p = head; p != NULL; p = p->next) /* WRONG */
       free(p);
The right way is to save whatever is needed before freeing:
   for (p = head; p != NULL; p = q) {
       q = p->next;
       free(p);
   }

C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Sam Chen
Extract filename from full path using perl
Encrypting Passwords in Tomcat using Servlets
malloc, calloc - Storage Management - in C
Character Pointers and Functions in C
Declarations in C
strcpy() and strncpy() sample program in C++
Sample program to draw a rectangle in J2ME
Design Patterns for Properties in a Java Bean
PushbackInputStream example program in Java
FileInputStream - sample program in Java

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Open, Creat, Close, Unlink system calls sample program in C
UNIX read and write system calls sample program in C
Using memset(), memcpy(), and memmove() in C
Character Counting sample program in C
register Variables in C
Listing Files and Directories sample program in C
Sum of the elements of an array in C
The Birth and history of C Programming Language
Functions in C
Data Types and Sizes in C
Variable Scope and functions in C
Pointers and Function Arguments in C
Pointers to Functions example in C
Basics of Structures in C
Pointers to Structures example program in C
Most Emailed Articles (in C)
Arguments - Call by Value in C
Increment and Decrement Operators in C
The if else statement in C
Do while Loops in C
Printing a simple histogram in C
The Birth and history of C Programming Language
Basics of C
Getting Started with C
Variables and Arithmetic Expressions in C
The for statement in C
Symbolic Constants using #define in C
File Copying in C
Character Counting sample program in C
Line Counting sample program in C
Word Counting sample program in C