fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. //------------------ Example 1: Basic Padding -----------------
  6. struct A {
  7. char a; // 1 byte
  8. int b; // 4 bytes -> requires alignment
  9. char c; // 1 byte
  10. };
  11.  
  12. //------------------ Example 2: Multiple Structures ------------
  13. struct S1 {
  14. char c;
  15. double d;
  16. int i;
  17. };
  18.  
  19. struct S2 {
  20. double d;
  21. int i;
  22. char c;
  23. };
  24.  
  25. struct S3 {
  26. char c;
  27. int i;
  28. double d;
  29. };
  30.  
  31. //------------------ Example 3: Packed Structure -----------------
  32. #pragma pack(1) // Disable padding
  33. struct Packed {
  34. char a;
  35. int b;
  36. char c;
  37. };
  38. #pragma pack() // Reset packing
  39.  
  40. //------------------ Example 4: Compare Normal vs Packed ---------
  41. struct Normal {
  42. char a;
  43. int b;
  44. char c;
  45. };
  46.  
  47. #pragma pack(1)
  48. struct Packed2 {
  49. char a;
  50. int b;
  51. char c;
  52. };
  53. #pragma pack()
  54.  
  55. //------------------ Main Function -------------------------------
  56. int main() {
  57. printf("------ EXAMPLE 1: Single Structure ------\n");
  58. printf("Size of struct A = %lu bytes\n\n", sizeof(struct A));
  59.  
  60. printf("------ EXAMPLE 2: Multiple Structures (ordering) ------\n");
  61. printf("Size of S1 = %lu bytes\n", sizeof(struct S1));
  62. printf("Size of S2 = %lu bytes (better ordering!)\n", sizeof(struct S2));
  63. printf("Size of S3 = %lu bytes\n\n", sizeof(struct S3));
  64.  
  65. printf("------ EXAMPLE 3: Packed Structure (disabled padding) ------\n");
  66. printf("Size of struct Packed = %lu bytes (packing forced)\n\n", sizeof(struct Packed));
  67.  
  68. printf("------ EXAMPLE 4: Normal vs Packed Comparison ------\n");
  69. printf("Size of Normal = %lu bytes\n", sizeof(struct Normal));
  70. printf("Size of Packed2 = %lu bytes\n", sizeof(struct Packed2));
  71.  
  72. printf("\nConclusion:\n");
  73. printf("- Structure padding exists to align data in memory for performance.\n");
  74. printf("- Reordering members can reduce padding.\n");
  75. printf("- #pragma pack(1) removes padding but makes access slower.\n");
  76.  
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
------ EXAMPLE 1: Single Structure ------
Size of struct A = 12 bytes

------ EXAMPLE 2: Multiple Structures (ordering) ------
Size of S1 = 24 bytes
Size of S2 = 16 bytes (better ordering!)
Size of S3 = 16 bytes

------ EXAMPLE 3: Packed Structure (disabled padding) ------
Size of struct Packed = 6 bytes (packing forced)

------ EXAMPLE 4: Normal vs Packed Comparison ------
Size of Normal  = 12 bytes
Size of Packed2 = 6 bytes

Conclusion:
- Structure padding exists to align data in memory for performance.
- Reordering members can reduce padding.
- #pragma pack(1) removes padding but makes access slower.