C में Memory Access
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक malloc Pointer Dereference करना
* से Dereference करना dynamically allocated memory को पढ़ता और लिखता दोनों है, और allocated memory को same pointer के जरिए बार-बार पढ़ा और modify किया जा सकता है जब तक यह valid रहे।
उदाहरण: Dereferencing a malloc Pointer
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक Allocated Array Access करना
कई elements रखने के लिए allocate की गई memory का एक block exactly एक regular array की तरह access किया जा सकता है, allocated block के अंदर हर individual element तक पहुँचने के लिए square-bracket indexing इस्तेमाल करते हुए।
उदाहरण: Accessing an Allocated Array
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Bounds Checking मायने रखता है
C कोई automatic bounds checking नहीं करता, इसलिए allocated size से आगे एक index access करना compile होता है लेकिन undefined behavior produce करता है, allocated size का careful track रखना programmer की responsibility बनाते हुए।
उदाहरण: Bounds Checking Matters
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Freed Memory Access करना Undefined है
एक बार memory free से release हो जाए, इसे refer करने वाला pointer एक dangling pointer बन जाता है, और बाद में इसे dereference करना undefined behavior है क्योंकि वह memory कहीं और already reuse हो चुकी हो सकती है।
उदाहरण: Accessing Freed Memory is Undefined
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- allocated size से आगे पढ़ना या लिखना, जैसे
nelements के array के लिएp[n], जो undefined behavior है। - यह check किए बिना
mallocका result dereference करना कि यहNULLनहीं है। freecall करने के बाद memory इस्तेमाल करना, जो एक dangling pointer छोड़ता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: