- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a malloc function in C language?
The malloc() function stands for memory allocation, that allocate a block of memory dynamically.
It reserves the memory space for a specified size and returns the null pointer, which points to the memory location.
malloc() function carries garbage value. The pointer returned is of type void.
The syntax for malloc() function is as follows −
ptr = (castType*) malloc(size);
Example
The following example shows the usage of malloc() function.
#include<stdio.h> #include<string.h> #include<stdlib.h> int main(){ char *MemoryAlloc; /* memory allocated dynamically */ MemoryAlloc = malloc( 15 * sizeof(char) ); if(MemoryAlloc== NULL ){ printf("Couldn't able to allocate requested memory
"); }else{ strcpy( MemoryAlloc,"TutorialsPoint"); } printf("Dynamically allocated memory content : %s
", MemoryAlloc); free(MemoryAlloc); }
Output
When the above program is executed, it produces the following result −
Dynamically allocated memory content: TutorialsPoint
Advertisements