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

C में Memory Access

Dynamically allocated memory को * से pointer dereference करके या array-style indexing इस्तेमाल करके access किया जाता है, और C कोई automatic bounds checking नहीं करता, इसलिए out-of-range या use-after-free access undefined behavior है।
Syntax
c
data_type *pointer_name = malloc(count * sizeof(data_type));

*pointer_name = value;
pointer_name[index] = value;

Allocated Memory Access करना

Dynamically allocated memory access करने का मतलब है dereference operator * या array-style square-bracket indexing इस्तेमाल करके एक pointer जिस address को refer करता है वहाँ store values पढ़ना या लिखना।

उदाहरण: Accessing Allocated Memory

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[0]);
	// Release the heap memory so it can be reused
	free(ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक malloc Pointer Dereference करना

* से Dereference करना dynamically allocated memory को पढ़ता और लिखता दोनों है, और allocated memory को same pointer के जरिए बार-बार पढ़ा और modify किया जा सकता है जब तक यह valid रहे।

उदाहरण: Dereferencing a malloc Pointer

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;
	*ptr = *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;
}

एक Allocated Array Access करना

कई elements रखने के लिए allocate की गई memory का एक block exactly एक regular array की तरह access किया जा सकता है, allocated block के अंदर हर individual element तक पहुँचने के लिए square-bracket indexing इस्तेमाल करते हुए।

उदाहरण: Accessing an Allocated Array

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 = malloc(3 * sizeof(int));
	arr[0] = 1; arr[1] = 2; arr[2] = 3;
	// Print formatted text to the screen
	printf("%d", arr[1]);
	// Release the heap memory so it can be reused
	free(arr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Bounds Checking मायने रखता है

C कोई automatic bounds checking नहीं करता, इसलिए allocated size से आगे एक index access करना compile होता है लेकिन undefined behavior produce करता है, allocated size का careful track रखना programmer की responsibility बनाते हुए।

उदाहरण: Bounds Checking Matters

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 = malloc(3 * sizeof(int));
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		arr[i] = i;
	}
	// Print formatted text to the screen
	printf("%d", arr[2]);
	// Release the heap memory so it can be reused
	free(arr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Freed Memory Access करना Undefined है

एक बार memory free से release हो जाए, इसे refer करने वाला pointer एक dangling pointer बन जाता है, और बाद में इसे dereference करना undefined behavior है क्योंकि वह memory कहीं और already reuse हो चुकी हो सकती है।

उदाहरण: Accessing Freed Memory is Undefined

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;
	// Release the heap memory so it can be reused
	free(ptr);
	ptr = NULL;
	// Print formatted text to the screen
	printf("%d", ptr == NULL);
	// Return 0 to signal that the program finished successfully
	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. allocated size से आगे पढ़ना या लिखना, जैसे n elements के array के लिए p[n], जो undefined behavior है।
  2. यह check किए बिना malloc का result dereference करना कि यह NULL नहीं है।
  3. free call करने के बाद memory इस्तेमाल करना, जो एक dangling pointer छोड़ता है।
🔒

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.