0% found this document useful (0 votes)
45 views

Function Malloc and Free

The malloc function allocates memory from the heap during program execution. It requests a block of memory from the operating system, which reserves the requested amount if available. The free function returns allocated memory to the operating system when it is no longer needed. Malloc can allocate memory for structures by specifying the size of the structure. It returns a pointer to the allocated block that is then used to assign values to structure members. Free must be called to release the memory when done.

Uploaded by

oraculus_lunae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Function Malloc and Free

The malloc function allocates memory from the heap during program execution. It requests a block of memory from the operating system, which reserves the requested amount if available. The free function returns allocated memory to the operating system when it is no longer needed. Malloc can allocate memory for structures by specifying the size of the structure. It returns a pointer to the allocated block that is then used to assign values to structure members. Free must be called to release the memory when done.

Uploaded by

oraculus_lunae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

C Tutorial The functions malloc

and free
The function malloc is used to allocate a certain amount of memory during the execution
of a program. The malloc function will request a block of memory from the heap. If the
request is granted, the operating system will reserve the requested amount of memory.
When the amount of memory is not needed anymore, you must return it to the operating
system by calling the function free.
Take a look at the following example:

#include<stdio.h>
int main()
{
int *ptr_one;
ptr_one = (int *)malloc(sizeof(int));
if (ptr_one == 0)
{
printf("ERROR: Out of memory\n");
return 1;
}
*ptr_one = 25;
printf("%d\n", *ptr_one);
free(ptr_one);
return 0;
}
Note: If you compile on windows the windows.h file should be included to use malloc.
The malloc statement will ask for an amount of memory with the size of an integer (32
bits or 4 bytes). If there is not enough memory available, the malloc function will return
a NULL. If the request is granted a block of memory is allocated (reserved). The address
of the reserved block will be placed into the pointer variable.
The if statement then checks for the return value of NULL. If the return value equals
NULL, then a message will be printed and the programs stops. (If the return value of the
program equals one, than thats an indication that there was a problem.)
The number twenty-five is placed in the allocated memory. Then the value in the
allocated memory will be printed. Before the program ends the reserved memory is
released.

Malloc and structures


A structure can also be used in a malloc statement.
Take a look at the example:

#include<stdio.h>
typedef struct rec
{
int i;
float PI;
char A;
}RECORD;
int main()
{
RECORD *ptr_one;
ptr_one = (RECORD *) malloc (sizeof(RECORD));
(*ptr_one).i = 10;
(*ptr_one).PI = 3.14;
(*ptr_one).A = 'a';
printf("First value: %d\n",(*ptr_one).i);
printf("Second value: %f\n", (*ptr_one).PI);
printf("Third value: %c\n", (*ptr_one).A);
free(ptr_one);
return 0;
}
Note: the

parentheses

around

*ptr_one

in

the

printf

statements.

This notation is not often used. Most people will use ptr_one->i instead. So (*ptr_one).i
= 25 and ptr_one->i = 25 are the same.
If you want to use the structure without the typedef the program will look like this:

#include<stdio.h>
struct rec
{
int i;
float PI;
char A;
};
int main()
{
struct rec *ptr_one;
ptr_one =(struct rec *) malloc (sizeof(struct rec));
ptr_one->i = 10;
ptr_one->PI = 3.14;

ptr_one->A = 'a';
printf("First value: %d\n", ptr_one->i);
printf("Second value: %f\n", ptr_one->PI);
printf("Third value: %c\n", ptr_one->A);
free(ptr_one);
return 0;
}
One last tip before we end the tutorial: Always use sizeof. Never use this notation
malloc(4). (Requesting 4bytes for the integer in the examples). This will make your code
much more portable.
If you look at the dynamic memory functions of the stdlib.h library you will see that there
are more functions that you can use to allocate dynamic memory. The following four
dynamic memory functions can be found in the stdlib.h library:

To
To
To
To

allocate space for an array in memory you use calloc()


allocate a memory block you use malloc()
reallocate a memory block with specific size you use realloc()
de-allocate previously allocated memory you use free()

Thats all for this tutorial.

You might also like