C में Dynamic Struct Allocation
In this page:
struct StructName *pointer_name = malloc(sizeof(struct StructName));
pointer_name->member = value;
free(pointer_name);
एक Struct Dynamically Allocate करना
एक struct को malloc(sizeof(struct StructName)) इस्तेमाल करके heap पर dynamically allocate किया जा सकता है, जो उस struct का एक instance रखने के लिए exactly काफी memory reserve करता है, एक pointer के जरिए accessed।
उदाहरण: Dynamically Allocating a Struct
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Define a structure type named Point
struct Point { int x; };
// Program execution starts in main()
int main() {
// Allocate memory on the heap
struct Point *p = malloc(sizeof(struct Point));
p->x = 5;
// Print formatted text to the screen
printf("%d", p->x);
// Release the heap memory so it can be reused
free(p);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
-> से Members Access करना
-> operator एक single step में एक pointer के जरिए struct का एक member access करता है, जो अन्यथा एक dereference के बाद dot operator होता, (*p).member की तरह लिखा गया।
उदाहरण: Accessing Members with ->
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Define a structure type named Point
struct Point { int x; };
// Program execution starts in main()
int main() {
// Allocate memory on the heap
struct Point *p = malloc(sizeof(struct Point));
p->x = 10;
// Print formatted text to the screen
printf("%d", p->x);
// Release the heap memory so it can be reused
free(p);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Structs का एक Array Allocate करना
sizeof(struct StructName) को desired element count से multiply करके structs का एक पूरा array एक साथ dynamically allocate किया जा सकता है, और array में हर struct फिर regular indexing से accessed होता है।
उदाहरण: Allocating an Array of Structs
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Define a structure type named Point
struct Point { int x; };
// Program execution starts in main()
int main() {
// Allocate memory on the heap
struct Point *points = malloc(3 * sizeof(struct Point));
points[1].x = 7;
// Print formatted text to the screen
printf("%d", points[1].x);
// Release the heap memory so it can be reused
free(points);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Dynamically Allocated Struct Free करना
एक dynamically allocated struct, चाहे एक single instance हो या इनका एक array, free की एक single call से release होता है, exactly malloc से मिले किसी भी दूसरे memory block की तरह।
उदाहरण: Freeing a Dynamically Allocated Struct
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Define a structure type named Point
struct Point { int x; };
// Program execution starts in main()
int main() {
// Allocate memory on the heap
struct Point *p = malloc(sizeof(struct Point));
p->x = 1;
// Release the heap memory so it can be reused
free(p);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Dynamically Allocated Members वाले Structs
जब एक struct में एक pointer member हो जो खुद separately allocated memory की ओर point करता है, जैसे एक dynamically sized name string, उस inner allocation को पहले free करना होगा, उसके बाद struct को खुद, inner memory leak होने से बचने के लिए।
उदाहरण: Structs with Dynamically Allocated Members
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Define a structure type named Person
struct Person { char *name; };
// Program execution starts in main()
int main() {
// Allocate memory on the heap
struct Person *p = malloc(sizeof(struct Person));
// Allocate memory on the heap
p->name = malloc(10);
// Release the heap memory so it can be reused
free(p->name);
// Release the heap memory so it can be reused
free(p);
printf("Freed inner then outer");
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक struct के pointer पर
->की बजाय.इस्तेमाल करना, जो compile नहीं होता। - struct इस्तेमाल करने से पहले यह check न करना कि
mallocनेNULLreturn किया। - struct free करना लेकिन इसके अपने pointer members जिनकी ओर point करते हैं वह memory free न करना, एक leak cause करते हुए।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: