← Back to C Course | Chapter 9: Memory Management | Lesson 2 of 8

C का malloc()

malloc program चलते समय computer से memory का एक हिस्सा मांगता है, एक restaurant में एक table borrow करने जैसा। आपको check करना होगा कि आपको मिला और खत्म होने पर इसे वापस देना होगा।
Syntax
c
#include <stdlib.h>

data_type *pointer_name = (data_type *) malloc(count * sizeof(data_type));

if (pointer_name == NULL) {
  // allocation failed
}

Dynamic Memory Allocation

यह stack memory से अलग है, जो function के return होने पर automatically sized और reclaim होती है -- heap memory तब तक रहती है जब तक आप इसे explicitly free न करें, और इसका size program के actually चलते हुए, real input के आधार पर decide किया जा सकता है।

उदाहरण: Dynamic Memory Allocation

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	// Allocate memory on the heap
	int *ptr = malloc(sizeof(int));
	*ptr = 5;
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Release the heap memory so it can be reused
	free(ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

malloc() Function

क्योंकि malloc() एक generic void pointer return करता है, आप आमतौर पर इसे चाहिए होने वाले type में cast करते हैं, जैसे int *arr = (int*)malloc(10 * sizeof(int)) -- ध्यान दें memory uninitialized होती है, जिसमें पहले जो भी garbage bytes थे वे होते हैं।

उदाहरण: The malloc() Function

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	// Allocate memory on the heap
	int *arr = (int*)malloc(10 * sizeof(int));
	arr[0] = 1;
	// Print formatted text to the screen
	printf("%d", arr[0]);
	// Release the heap memory so it can be reused
	free(arr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Allocation Success Check करना

production code में यह check optional नहीं है -- एक failed malloc() जो NULL return करता है उसे dereference करना आपके program को तुरंत crash कर देता है, और limited memory वाले systems पर, allocation failures एक real possibility हैं, सिर्फ एक theoretical edge case नहीं।

उदाहरण: Checking Allocation Success

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	// Allocate memory on the heap
	int *ptr = malloc(sizeof(int));
	// Check whether ptr == NULL
	if (ptr == NULL) {
		// Print formatted text to the screen
		printf("Allocation failed");
		// Send 1 back to the caller
		return 1;
	}
	*ptr = 5;
	// Print formatted text to the screen
	printf("%d", *ptr);
	free(ptr);
	return 0;
}

free() से Memory Free करना

यह step skip करना एक memory leak cause करता है: allocated block इसका pointer खो देते ही unreachable हो जाता है (जैसे pointer variable scope से बाहर चला जाए), लेकिन operating system program के exit होने तक उस memory को अभी भी 'in use' मानता है।

उदाहरण: Freeing Memory with free()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	// Allocate memory on the heap
	int *ptr = malloc(sizeof(int));
	*ptr = 10;
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Release the heap memory so it can be reused
	free(ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

malloc() से Dynamic Arrays

Multiplication (count * sizeof(type)) यह पक्का करता है कि आप target platform के specific type sizes की परवाह किए बिना exactly सही संख्या में bytes request करें, जो systems के बीच अलग हो सकते हैं -- एक byte count hardcode करने की बजाय हमेशा sizeof इस्तेमाल करें।

उदाहरण: Dynamic Arrays with malloc()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	// Declare count and set it to 5
	int count = 5;
	// Allocate memory on the heap
	int *arr = malloc(count * sizeof(int));
	// Loop: repeat while the condition holds
	for (int i = 0; i < count; i++) {
		arr[i] = i;
	}
	// Print formatted text to the screen
	printf("%d", arr[4]);
	// Release the heap memory so it can be reused
	free(arr);
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. memory इस्तेमाल करने से पहले यह check न करना कि malloc ने NULL return किया या नहीं।
  2. free call करना भूल जाना, ताकि memory program खत्म होने तक leak होती रहे।
  3. n * sizeof(type) की बजाय n elements चाहिए होने पर n bytes allocate करना।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.