当前位置:网站首页>Sizeof calculation space size summary
Sizeof calculation space size summary
2022-06-12 14:15:00 【zou_ albert】
About sizeof Summary of
1、sizeof Use form of :sizeof(var_name) perhaps sizeof var_name
2、 Variable names can be enclosed without parentheses :sizeof a
3、 Data types must be enclosed in parentheses :sizeof(int)
4、sizeof Expressions can be evaluated :sizeof(2+3.14), the truth is that sizeof(double)
5、sizeof Function calls can be evaluated , It is actually evaluating the return value type
6、 The following situations cannot be used sizeof Is evaluated
1) Function names cannot be evaluated
2) Cannot evaluate the type of an indefinite return value , Such as void
3) Bit field members cannot use sizeof evaluation
7、sizeof The result is size_t, It is defined as unsigned int type . This type ensures that the byte size of the largest object created by the display can be accommodated .
commonly , stay 32 Bit compiler :
sizeof(int):4
sizeof(short):2
sizeof(long):4
sizeof(long long):8
sizeof(float):4
sizeof(double):8
sizeof(char):1
sizeof(p):4,(p For the pointer )
These values vary from system to system .
8、sizeof And strlen() Compare , When calculating a character array ,sizeof contain ’\0’,strlen() It doesn't contain ’\0’
9、32 Of the pointer under the bit system sizeof yes 4 byte ,64 Next is 8 byte
10、 Reference to sizeof, In fact, it is for the object to which it is bound sizeof
11、 about C character string , It needs to be remembered C/C++ One Chinese character takes up two bytes (Linux Next 3 Bytes ), And there is an empty character at the end of the string .
12、 An array sizeof You can calculate the size of the array
13、 Array as a parameter , The name of the array sizeof yes 4 Bytes
14、32bit In the system ,int **a[3][4];sizeof(a)=48
15、 About struct Space computing for
struct The space calculation of is complex , Generally, two rules are followed :
1) stay windows32 In the environment , The overall space is the member that occupies the largest space ( The type of ) An integer multiple of the occupied bytes , stay Linux+gcc In the environment , If the number of bytes occupied by the maximum member type exceeds 4, Such as double yes 8, Then the whole is empty Between is 4 An integer multiple of .
2) stay windows32 In the environment , Data vs. its principles —— The sequence of memory structure members , When it comes to the member variable , The size of the space placed in front of it must be an integer multiple of the size of the member type , If not enough Then make up , And so on . But in Linux+gcc In the environment , If the member type exceeds 4, Such as double yes 8, The size of the space already placed in front is 4 An integer multiple of , If it is not enough, it will be completed .
win32 In the environment : When it's placed , The train of thought is :
1、 First place the first member at the address 0 The place of
2、 Then find the starting position of the second member according to its type size , The starting position of placement must be an integer multiple of the type size , By analogy .
3、 Last , Then calculate whether the overall space is an integer multiple of the maximum type size .
And in the Linux+gcc In the environment : The idea of placement is
1、 First place the first member at the address 0 The place of
2、 If one unit cannot be installed , Then it is placed at the starting position of a unit address .
3、 Last , If the maximum type band exceeds 4, Then calculate whether the whole space is 4 Integer multiple .
16、 Space calculation of structures containing structures :
1) stay windows32 In the environment , The whole space is between the child structure and the parent structure , The most occupied member ( The type of ) An integer multiple of the occupied bytes , stay Linux+gcc In the environment , If the number of bytes occupied by the maximum member type exceeds 4, Such as double yes 8, Then the overall space is 4 An integer multiple of .
2) stay windows32 In the environment , Data vs. its principles —— The memory of the parent structure is arranged in the order of the structure members , When ranking to substructure members , The space placed in front of it must be the largest in the substructure members An integer multiple of the size of a large type , If it is not enough, it will be completed , One analogy .
win32 In the environment : When it's placed , The train of thought is :
1、 First place the first member at the address 0 The place of
2、 Then find the starting position of the second member according to its type size , The starting position of placement must be an integer multiple of the type size , If it is a substructure , Then the starting position of the placement is the maximum type size in the substructure Integer multiple , By analogy .
3、 Last , Then calculate whether the overall space is an integer multiple of the maximum type size in the parent-child structure .
Linux+gcc Save memory , Type size exceeds 4, The starting position should be 4 An integer multiple of .
17、 Space calculation of structures containing arrays :
In the structure , Arrays are placed one by one according to a single variable , Not as a whole , Therefore, the alignment of structures containing arrays is quite special .
Such as :
struct S1
{
char a[8];
int b;
};
sizeof(S1)=12, instead of 16.
18、 Calculation of structures containing potential fields
Bit field members cannot be deleted alone sizeof value , But structures containing bit fields can .
There are the following rules for using bit fields :
1) If the types of adjacent bit fields are the same , And the sum of its bit width is less than that of type sizeof size , Then the following field will be immediately adjacent to the storage of the previous field , Until you know you can't hold it .
stay vs2010 Next , One int a:4, If the following is not a bit field , Then occupy 4 Bytes , That is, the size of its type . And in the Dec-C++ And gcc Next , Whether it's a bit field or not , The number of bytes occupied shall be subject to the actual number of bytes occupied , namely int a:4, If the following is not a bit field , Only one byte .
2) If the types of adjacent bit fields are the same , But the sum of its bit width is greater than that of its type sizeof size , The following fields will start with the new storage unit , Its offset is an integral multiple of the size of its type .
3) If the types of adjacent bit fields are different , Different compilers have different implementations ,VC6 No compression , and Dec-C++ And gcc Use compression .
4) If there are non bit field fields interspersed between bit field fields , No compression .
5) The total size of the whole structure is an integer multiple of the size of the fastest basic type member .
19、 Use "#pragma pack" Time structure space calculation
Generally, you can change the default conditions by the following methods :
Using pseudo instructions #pragma pack(n), The compiler will follow n Byte alignment ;
Using pseudo instructions #pragma pack(n), Custom byte alignment will be canceled .
The above two instructions should usually be used together .
#pragma pack(n),n Is the number of byte alignments , Its value is 1、2、4、8、16 etc. , The default is 8, If this value is greater than the sizeof Small value , The offset of the member should be based on this , That is, the offset of the structure member should be Take the minimum of the two . The formula is as follows :
offsetof(item)=min(n,sizeof(item))
20、union Space computing for
Of the consortium sizeof Value is all members sizeof The maximum of .
21、 Enumerative spatial computation
enum It just defines a set of constants , There's no “ Elements ”, Enumeration is used as int Type stored , Therefore, the sizeof The value is 4.
边栏推荐
- Alibaba cloud development board haas510 connects to the Internet of things platform -- Haas essay solicitation
- Knowledge of wireless card driver
- Mémoire de l'examen d'entrée à l'université
- chrome://tracing Performance analysis artifact
- QA of some high frequency problems in oauth2 learning
- SystemC uses SC_ report_ Handler processing log printing
- Reverse analysis from x86 to x64tips
- [semidrive source code analysis] [x9 chip startup process] 25 - Introduction to mailbox inter core communication mechanism (code analysis) rpmsg-ipcc RTOS & QNX
- TestEngine with ID ‘junit-vintage‘ failed to discover tests
- Dismantle and modify the advertising machine - Amateur decompression
猜你喜欢

SystemC common errors

Player practice 20 unpacking thread

阿里云开发板HaaS510响应UART串口指令

Conversion of player's actual 10 pixel format and size

单总线温度传感器18B20数据上云(阿里云)

【mysql进阶】索引分类及索引优化方案(五)

Communication flow analysis

Pay attention to click and pursue more users to enter the website. What bidding strategy can you choose?

Player practice 18 xresample

2022版Redis数据删除策略
随机推荐
Running phase of SystemC
[wustctf2020] selfie score query -1
Remote code injection
阿里云开发板HaaS510将串口获取数据发送到物联网平台
PostgreSQL14安装使用教程
Lua tvalue structure
注重点击,追求更多用户进入网站,可以选择什么出价策略?
Alibaba cloud development board haas510 submission device attributes
Binary tree traversal
Brush one question every day /537 Complex multiplication
动态搜索广告智能查找匹配关键字
Now you must know the pointer
Programmer interview golden classic good question / interview question 01.05 Edit once
【视频课】android studio物联网APP设计制作全套教程--国庆期间全掌握
What is automatic bidding? What are its advantages?
SystemC uses SC_ report_ Handler processing log printing
3. Process concealment under the ring ----- continuous concealment and new opening prevention
Interview question 17.14 Minimum number of K (almost double hundreds)
Player actual combat 14 display YUV
chapter19 Allocation