C में Struct Padding
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Field Order Total Size को Affect करता है
एक struct के members को reorder करना इसका total size घटा या बढ़ा सकता है भले ही fields खुद न बदले हों -- बड़े types को साथ group करना और char जैसे छोटों को अंत में रखना आमतौर पर compiler को insert करनी पड़ने वाली padding minimize करता है।
उदाहरण: Field Order Affects Total Size
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
जब मायने रखे तो 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि एक struct का
sizeofइसके members के sum के बराबर है, जब compiler alignment के लिए padding bytes add करता है। - members को गलत order में रखना, जैसे दो
ints के बीच एकchar, जो padding में memory waste करता है। - यह मान लेना कि 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: