malloc, calloc , realloc and free

Dynamic Memory Allocation

"stdlib.h" header file include in your program.


malloc()

malloc stands for "memory allocation".

It allocate space in memory at run time according to given size.

It allocate space in single block.

Syntax

ptr = (cast-type*) malloc(byte-size);

NOTE: In above syntax ptr is a pointer type variable.

NOTE: If malloc() allocate space then it return pointer otherwise NULL.


free()

It is used to release run time allocated memory. It means free() can frees occupied memory of malloc() or calloc().

Syntax

free(ptr);

See Output

calloc()

calloc stands for "contiguous allocation".

calloc() allocates multiple block of memory.

Difference between malloc() and calloc():- malloc() allocates single block of memory but calloc() allocates multiple block of memory.

Syntax

ptr = (cast-type*)calloc(n, element-size);

See Output

realloc()

If calloc() or malloc() allocated memory is not sufficient for requirement then we can change size of memory by realloc() at run time.

Here, ptr is reallocated with size of newsize.

Syntax

ptr =(cast-type*)realloc(ptr, newsize);

See Output

About Us

Comsysapp.com is an educational website. Students and software developers can learn programming language tutorials. Comsysapp is very useful for beginners and professional developers. Comsysapp provides tutorial in easy language. Comsysapp.com has focus on simplicity.

Services

Comsysapp.com provides free tutorials like c, html, css, etc. All tutorials are free for beginner and professionals.


Terms of Use

comsysapp.com is not responsible for any mistake. We are not responsible if information made available on our website is incomplete or invalid. But comsysapp.com always try for zero-zero mistake.

Privacy policy

comsysapp.com does not collect any data from users. We use Google AdSense advertising on our website. We never ask personal or private information.


Sitemap

sitemap

copyright © 2023