sizeof operator and the introduction of common pit

First, the basic concept

sizeof C language is a keyword, which is used to calculate the variables (or data type) occupies the number of bytes of memory in the current system.

sizeof is not a function, such a question is because writing is a bit like the sizeof function, sizeof there are two writing:

The type of data

  sizeof(数据类型);

Data types must be enclosed in parentheses.

  printf("字符型变量占用的内存是=%d\n",sizeof(char));   // 输出:字符型变量占用的内存是=1
  printf("整型变量占用的内存是=%d\n",sizeof(int));   // 输出:整型变量占用的内存是=4

For variable

  sizeof(变量名);
  sizeof 变量名;

Variable names can not enclosed in brackets, parentheses usage is more common, most programmers use this form.

  int ii;
  printf("ii占用的内存是=%d\n",sizeof(ii));   // 输出:ii占用的内存是=4
  printf("ii占用的内存是=%d\n",sizeof ii);   // 输出:ii占用的内存是=4

Second, note

1, sizeof (structure)

Various members of the theory in terms of structure in memory is stored in a row, and arrays are very similar, however, take up memory structure does not necessarily equal the total size of all the member variables and the size of the occupied memory. In a particular realization of the compiler, to improve the efficiency of memory addressing, there may be a gap between various members. Content can be obtained by the structure occupying in the total size sizeof, sizeof (name structure) or sizeof (variable name structure) may be used.

Example (book90.c)

/*
 * 程序名:book90.c,此程序用于演示C语言的结构体占用内存的情况
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

// #pragma pack(1)  # 告诉编器内存按1字节对齐。

struct st_girl
{
  char name[50];     // 姓名
  int  age;          // 年龄
  int  height;       // 身高,单位:厘米cm
  char sc[30];       // 身材,火辣;普通;飞机场。
  char yz[30];       // 颜值,漂亮;一般;歪瓜裂枣。
};

int main()
{
  struct st_girl queen;
  printf("sizeof(struct st_girl) %d\n",sizeof(struct st_girl));
  printf("sizeof(queen) %d\n",sizeof(queen));
}

running result
Here Insert Picture Description

As can be seen from the above example, struct st_girl memory occupied by all member variables is 50 + 4 + 4 + 30 + 30 = 118, but the structure is occupied by the memory 120.

Note that, C language is provided a method of memory alignment structure member, the structure can be made no gap between the members memory variable to enable #pragma pack (1) code.

2, do not use sizeof to void

  printf("sizeof(void)=%d\n",sizeof(void));  // 输出sizeof(void)=1

In the above code in some compilers may not pass.

void-free type or a null value type, do not know the type of storage space, the compiler can not determine its size. Variables can not be declared void, the following code can not be compiled by:

  void vv;

But the following code is correct:

  void *pv;
  printf("sizeof(void*)=%d\n",sizeof(pv));   // 输出sizeof(void)=8

pv is a void pointer, the 64-bit operating systems, the pointer variable is the size of memory occupied by 8, the same below.

3. Do not use sizeof character pointer in the subroutine

If a character string (e.g., char strname [21]) of the address to the subroutine, subroutine with a pointer to a character (e.g., char * pstr) to store the incoming address string, if the sub-function with sizeof ( pstr), the number of bytes of memory for the string is not obtained, but the character pointer variable number of bytes of memory occupied (8 bytes).

Therefore, you can initialize subroutine incoming string, unless the length of the string to be passed as parameters in the subroutine.

4, not to use a structure pointer in the subroutine while sizeof

If the address of a structure (e.g. struct st_girl stgirl) passed to subroutine, subroutine with a pointer to a structure (e.g. struct st_girl * pgril) to store the incoming address of the structure, if the sub-functions with sizeof ( pgirl), the resulting structure is not occupied by the number of bytes of memory, but occupy a structure pointer variable number of bytes of memory (8 bytes). The correct usage is to use sizeof (struct st_girl).

Third, copyright notice

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.

Source: C Language Technology Network ( www.freecplus.net )

Author: Ethics Code farming

If this article helpful to you, please praise support, or forwarded my article in your blog, thank you.

Published an original article · won praise 2 · Views 8337

Guess you like

Origin blog.csdn.net/u010806950/article/details/105315935