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

C में Dynamic Struct Allocation

एक struct को malloc(sizeof(struct Name)) से heap पर allocate किया जा सकता है, -> operator से access किया जा सकता है, और किसी भी दूसरे block की तरह free किया जा सकता है, nested pointer members को अपनी अलग allocation और free चाहिए।
Syntax
c
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

c
// 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;
}

-> से Members Access करना

-> operator एक single step में एक pointer के जरिए struct का एक member access करता है, जो अन्यथा एक dereference के बाद dot operator होता, (*p).member की तरह लिखा गया।

उदाहरण: Accessing Members with ->

c
// 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;
}

Structs का एक Array Allocate करना

sizeof(struct StructName) को desired element count से multiply करके structs का एक पूरा array एक साथ dynamically allocate किया जा सकता है, और array में हर struct फिर regular indexing से accessed होता है।

उदाहरण: Allocating an Array of Structs

c
// 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;
}

एक Dynamically Allocated Struct Free करना

एक dynamically allocated struct, चाहे एक single instance हो या इनका एक array, free की एक single call से release होता है, exactly malloc से मिले किसी भी दूसरे memory block की तरह।

उदाहरण: Freeing a Dynamically Allocated Struct

c
// 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;
}

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

c
// 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;
}
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. एक struct के pointer पर -> की बजाय . इस्तेमाल करना, जो compile नहीं होता।
  2. struct इस्तेमाल करने से पहले यह check न करना कि malloc ने NULL return किया।
  3. 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:

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.