In case of static memory allocation there are some drawbacks like: wastage of memory, less flexibility, permanent allocation of variables etc. To overcome these situations the concept of Dynamic Memory Allocation has been introduced. In dynamic memory allocation the memories can be allocated dynamically at run time.
There 4 library functions that are described under <stdlib.h> (Standard Library Functions)
1. malloc()
2. calloc()
3. realloc()
4. free()
In this article we’ll deeply read about malloc().
What is malloc()?
To allocate memory dynamically we use the function which is called as malloc() function. malloc() function on success returns the base address of the allocated memory on failure it returns the NULL value. Every memory location will be initialized with a garbage value.
Memory allocation fails if sufficient memory is not provided. Unsuccessful memory allocation returns a NULL pointer (nullptr) to the pointer.
Syntax:
ptr = (datatype *)malloc((byte_size)*(number of elements))
For example, ptr = (int *)malloc(sizeof(int)*10)
It will create an array of 10 elements and will return the base address of the array. Every memory location will be initialized with a garbage value. This above statement will allocate 40 bytes of memory.

Example
/* C program to understand how malloc() works in DMA */
/* www.codewindow.in */
#include <stdio.h>
#include <stdlib.h>
// Driver code
int main() {
int n, i;
int *p; // Integer pointer is declared
printf("Enter a valid range: ");
scanf("%d", &n);
p=(int*)malloc(n*sizeof(int)); // An int array of n elements has been assigned to the pointer
if(p == NULL) { // Checking if memory allocation is successful or not
printf("\nMemory allocation unsuccessful");
exit(0);
}
else { // Else memory allocation is successful
printf("\nMemory allocation successful");
printf("\nEnter the elements of array: ");
for(i=0; i<n; ++i)
scanf("%d", p+i); // Array elements are taken as input
printf("\nElements of the array are: ");
for(i=0; i<n; i++)
printf("%d ", *(p+i)); // Print element of the array
}
return 0;
}
Input
Enter a valid range: 10
Enter the elements of array: 1 2 3 4 5 6 7 8 9 10
Output
Memory allocation successful
Elements of the array are: 1 2 3 4 5 6 7 8 9 10
Follow Us
You Missed
- Betsafe Sports betting Review 2025 Is Betsafe Sportsbook Legitimate?
- Betsafe Extra Offer The new Athlete Rating Code a hundred% as much as $500 March 2025
- Lucky Days Spielbank Nachprüfung, 100% up to $100, 100 Spins Steckplatz Fruitinator Sign Up Provision
- Fantastisk Safari Online Slot casino stars tilmeldingsbonus Research Prøv Fr Eller Med Rigtige Middel
- triple anal bbc extreme and big ass fantasy hardcore hd
- Noxwin Gambling enterprise Comment 2024 Dr Joan E. Hughes Technical Combination Search
- Finest Online slots 2025 Play Harbors the real deal Money
- Dolphin’s Pearl Trial Enjoy Free Slot Video game
- Whales Pearl Slots Play the Demo On the internet for free
- Top Bitcoin Online casinos the real deal Currency United states: BTC Local casino
- Greatest No-deposit Gambling enterprise Incentives 2024 » Free Bucks & 100 percent free Spins
- ten Finest International Online casino Websites: Planet’s Top Casinos
- Greatest Roulette Web sites & Incentives January 2025
- UKG Hiring for Software Engineer
- New jersey On the internet Gambling establishments These Acknowledge Paypal
- 500% On line casino Bonus products【2022】 Privately owned d% Casino Benefit
- *new* 54 African On the net On line casinos With out Put in Bonus deals 2022
- Unlocking Innovation and Diversity: Accenture HackDiva Empowers Women in Tech with Cutting-Edge Solutions – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- Software Engineer Positions at Siemens Company: Apply Now – codewindow.in
- Cloud Engineer II Opportunities at Insight Company: Apply Now – codewindow.in
- Shape Your Career: Assistant Engineer Opportunities at Jindal Company – codewindow.in
- Shape Your Future: Executive Opportunities at Jindal Company – cdewindow.in
- Associate Engineer, Software Development at Ingram: Apply Now – codewindow.in
- Jade Company’s UI/UX Development Engineer Opportunities – Apply Now – codewindow.in
- Transform Your Career with S&P Global: Apply for the Software Development Engineer Role and Lead the Future of Financial Technology Innovation – codewindow.in
- Unlock Your Potential at Accenture as an Associate Software Engineer – Elevate Your Career with Innovation and Excellence – codewindow.in
- Accelerate Your Career: Join NVIDIA’s Elite Software Engineering Internship Program and Shape the Future of Technology – codewindow.in
- C Programming Interview Questions – codewindow.in
- Lead the Way in Analytics: Specialist Position at Razorpay – codewindow.in
- Innovate with Cyient: Junior Software Engineer Wanted – codewindow.in
Also Checkout
- Adobe (1)
- Advanced Coading (1)
- Advanced course (1)
- Ajax (17)
- Algorithm (6)
- Amagi (1)
- Amazon Interview Questions (1)
- Angular JS (23)
- Aptitude (10)
- Aptitude tricks (3)
- Automata Fixing (1)
- Basic Coding (1)
- big data (61)
- blog (1)
- Books (1)
- Bridge2i (1)
- C programming (20)
- Campgemini Interview Questions (1)
- Capgemini Coding Questions (2)
- Capgemini Pseudocode (4)
- Celebal Tech (1)
- Cloud Computing (28)
- code nation (2)
- Coding Questions (240)
- Cognizant Placement (11)
- commvault Systems (1)
- Computer Network (24)
- CSS (44)
- CTS (1)
- Data Science (44)
- Data Structure (1)
- Data Structure and Algorithm (126)
- DBMS (29)
- De Show Interview Questions (1)
- deloitte (2)
- Deutsche Bank Interview questions (1)
- Enhance Communication (1)
- Epam Full Question Paper (6)
- Extempore (1)
- Exxon Mobil interview questions (1)
- filpkart (1)
- Fractal Analytics Interview Questions (1)
- Genpact (1)
- Grab (1)
- GreyB Interview Questions (1)
- Group Discussion (1)
- Guidance for Accenture (3)
- Gupshup (1)
- Hackathon 2024 (1)
- HCL Interview Questions (1)
- Hexaware (1)
- HFCL (1)
- HR Questions (11)
- HTML5 (44)
- IBM Interview questions (1)
- IBM Questions (5)
- Incture Interview Questions (1)
- Infineon Technologies Interview Questions (1)
- Infosys (11)
- Infosys Interview Questions (1)
- Internship (1)
- Interview Experience (19)
- Interview Questions (63)
- ITC Infotech (1)
- itron (1)
- JavaScript (67)
- JECA (1)
- Job Info (93)
- JQuery (15)
- Kantar Interview Questions (1)
- Language Confusion (1)
- language confussion (1)
- Larsen & Turbo (1)
- Latenview AnalyticsInterview questions (1)
- Lexmark International Interview Questions (1)
- Linkedin (1)
- Machine Learning (23)
- Media.net (1)
- Mindtree Interview Questions (1)
- Miscellaneous (61)
- Mock Test Series (2)
- MongoDB (34)
- Morgan Stanly Interview Questions (1)
- nagarro (5)
- navi (1)
- NodeJS (24)
- NTT Data Interview Questions (1)
- NVDIA (1)
- NVDIA interview questions (1)
- Operating System (19)
- Optum (1)
- PayU (2)
- Persistent INterview Questions (1)
- PHP and MYSQL (31)
- Previous Coding Questions (1)
- Programming in C (61)
- Programming in C++ (33)
- Programming in JAVA (154)
- Programming in Python (133)
- Pseudo Code (2)
- pseudocode (5)
- PWC Interview Questions (1)
- Python (61)
- Quiz (9)
- Razorpay (1)
- ReactJS (26)
- Recruiting Companies (34)
- Revature (3)
- salesforce (1)
- Samsung (1)
- Schlumberger (1)
- Seimens (2)
- Slice (1)
- Smart Cube (1)
- Software Engineering (35)
- Study Material (4)
- Tally Solutions (1)
- tata cliq (1)
- TCS (1)
- TCS NQT (69)
- TCS NQT Coding Questions (13)
- Tech Mahindra Coding Questions (4)
- Tech Mahindra Questions (8)
- Technical Preparation (1)
- Teg Analytics (1)
- Tejas Network Interview Questions (1)
- Texas Instrument Interview Questions (1)
- Tiger Analytics (1)
- Uncategorized (83)
- UnDosTres (1)
- Unstop (1)
- Verbal Ability (8)
- Verbal Lesson (1)
- Web Development (208)
- wipro (1)
- Wipro Coding Questions (5)
- Wipro interview Questions (1)
- Wipro NLTH (30)
- WIpro NLTH Coding Solve (19)
- Zenser (1)
- Zoho Interview Questions (1)