← Back to C Course | Chapter 8: Structures & Unions | Lesson 7 of 7

C में Struct Padding

Struct padding वह खाली space है जो C एक structure के अंदर add करता है ताकि इसके parts memory में neatly line up हों, items सही fit करने के लिए एक shelf में gaps की तरह। यह structure को थोड़ा बड़ा बनाता है।
Syntax
c
struct StructName {
    char member1;   // 1 byte, then padding
    int member2;    // 4 bytes, aligned to a 4-byte boundary
};

printf("%zu", sizeof(struct StructName));   // size includes padding

Padding क्यों Exist करता है

ज़्यादातर CPUs multi-byte values सबसे तेज़ तब पढ़ते हैं जब वे अपने size का एक multiple होने वाले एक memory address पर बैठे हों -- एक 4-byte int सबसे तेज़ 4 से divisible एक address से पढ़ा जाता है -- इसलिए compiler struct members के बीच unused padding bytes insert करता है हर field को इस तरह aligned रखने के लिए।

उदाहरण: Why Padding Exists

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Example
struct Example {
	char c;
	int i;
};
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%zu", sizeof(struct Example));
	// Return 0 to signal that the program finished successfully
	return 0;
}

sizeof से Padding देखना

एक char के बाद एक int वाला struct अक्सर इसके members के individual sizes के sum से बड़ा sizeof report करता है -- compiler ने char के बाद चुपचाप 3 padding bytes insert किए ताकि अगला int एक 4-byte boundary पर land करे।

उदाहरण: Seeing Padding with sizeof

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Mixed
struct Mixed {
	char c;
	int i;
};
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%zu %zu %zu", sizeof(char), sizeof(int), sizeof(struct Mixed));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Field Order Total Size को Affect करता है

एक struct के members को reorder करना इसका total size घटा या बढ़ा सकता है भले ही fields खुद न बदले हों -- बड़े types को साथ group करना और char जैसे छोटों को अंत में रखना आमतौर पर compiler को insert करनी पड़ने वाली padding minimize करता है।

उदाहरण: Field Order Affects Total Size

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Ordered
struct Ordered {
	int i;
	char c;
};
// Define a structure type named Reordered
struct Reordered {
	char c;
	int i;
};
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%zu %zu", sizeof(struct Ordered), sizeof(struct Reordered));
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक Struct के अंत में Padding

Compilers एक struct के last member के बाद trailing padding भी add करते हैं ताकि struct का overall size इसके सबसे strict member की alignment requirement का एक multiple हो, जो तब मायने रखता है जब struct एक array के अंदर इस्तेमाल हो ताकि हर element properly aligned रहे।

उदाहरण: Padding at the End of a Struct

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Trailing
struct Trailing {
	int i;
	char c;
};
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%zu", sizeof(struct Trailing));
	// Return 0 to signal that the program finished successfully
	return 0;
}

जब मायने रखे तो Padding Disable करना

#pragma pack(1) जैसे compiler-specific directives एक struct को बिल्कुल बिना padding के packed होने के लिए force कर सकते हैं, जो तब मायने रखता है जब एक exact binary file format या network protocol layout match करना हो, लेकिन इस तरह unaligned fields पढ़ना कुछ hardware पर धीमा या fault भी हो सकता है।

उदाहरण: Disabling Padding When It Matters

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
#pragma pack(1)
// Define a structure type named Packed
struct Packed {
	char c;
	int i;
};
#pragma pack()
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%zu", sizeof(struct Packed));
	// 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. यह मान लेना कि एक struct का sizeof इसके members के sum के बराबर है, जब compiler alignment के लिए padding bytes add करता है।
  2. members को गलत order में रखना, जैसे दो ints के बीच एक char, जो padding में memory waste करता है।
  3. यह मान लेना कि padding layout हर compiler और platform पर identical है।
चैप्टर सारांश
  • Structures अलग types के related variables को एक नाम के तहत group करते हैं, और दूसरे structures के अंदर nested हो सकते हैं।
  • Structures को functions को pass किया जा सकता है और pointers से access किया जा सकता है।
  • Unions एक memory area share करते हैं, enumerations integer constants को नाम देते हैं, और struct padding इस पर असर डालता है कि एक structure कैसे lay out होता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.