malloc, calloc - Storage Management - in C

By: Sam Chen Emailed: 1696 times Printed: 2196 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 )
Using memset(), memcpy(), and memmove() in C
UNIX read and write system calls sample program in C
lseek() sample program in C
Character Arrays in C
Macro Substitution using #define in C
Pointers and Arrays in C
Sum of the elements of an array in C
Printing a simple histogram in C
Find square and square root for a given number in C
Symbolic Constants using #define in C
Conditional Expressions in C
The if else statement in C
getch and ungetch in C
register Variables in C
Initialization of Variables in C
Most Emailed Articles (in C)
Symbolic Constants using #define in C
Address Arithmetic and pointers in C
Tutorial on Complicated Declarations in C
lseek() sample program in C
Using malloc() Function in C
Bitwise Logical Operators in C
Getting Started with C
External Variables and Scope in C
Data Types and Sizes in C
The if else statement in C
else if statement in C
While and For Loops in C
A C program similar to grep command in UNIX
Variable Scope and functions in C
register Variables in C