The Overlooked Corner of C Language – #pragma pack

PS: Please indicate the source when reprinting, all rights reserved by the author.PS: This is just based on my own understanding,if it conflicts with your principles and ideas, please forgive me, do not criticize.

Environment Description

  None

Introduction

  Preface: I am a lazy person, bored and idle. While browsing the internet, I accidentally discovered something related to C (as seen in my related articles). During my in-depth study, I encountered #PRAGMA PACK([pop push n ……]), which left me puzzled.

  Everyone knows that when we write a C program, just like programs in other languages, it goes from Source Code –> Object Code –> Executable File. There are many details (default properties, etc.) that are obscured by one thing – the compiler and linker.

  The roles of the compiler and linker are:

  • • First, translate
  • • Second, analyze
  • • Finally, generate the executable file.

  Here, since we are not discussing some common knowledge about compilers, such as syntax trees, character streams, lexical syntax analysis, etc., we will talk about something we have not focused on, which is the allocation of logical addresses for variables and procedures in our program. Logical addresses and starting addresses all begin at 0. So what about the logical addresses of our other addresses? In addition to the address of the procedure segment, there is also the issue of variable addresses. Today, we will take a look at the issue of variable addresses.

  As far as I know, the C language divides the memory size of variables based on the size of the int type. In a 32-bit environment, int is 4 bytes. This is a very important reference for alignment. Memory address alignment is crucial. The advantages are that it can improve access speed and reduce design difficulty. The disadvantages are that it may waste memory in some cases.

  In the compiler (for 32-bit machines), the default alignment is 4 bytes. If you want to change this alignment size (as for why it would change, aside from some calculations related to structures, due to the author’s limited knowledge, I cannot comment on other aspects).

#pragma pack()

#pragma pack() is used to adjust the alignment of your code during compilation.

Example: Suppose on a 16-bit platform, char is one byte, and int is 2 bytes in size.

#pragma pack(push)

#pragma pack(2)


typedef struct {
    char a;
    int b;
} test;

#pragma pack(pop)


xxx.c file:

test mmm;
int d = &mmm.a - &mmm.b;


What is the value of d? Can you calculate it? If you have your own thoughts, then you have a certain understanding of #pragma pack.

Postscript

  None

References

  None

Donations, subscriptions, favorites, throwing bananas, coins, please follow the public accountThe Overlooked Corner of C Language - #pragma pack

PS: Please respect originality, if you don’t like it, don’t criticize.PS: Please indicate the source when reprinting, all rights reserved by the author.PS: If you have any questions, please leave a message, I will reply as soon as I see it.

Leave a Comment