The usage of sizeof in C language

Source: Internet
Author: User

Today, my classmates asked me whether sizeof can calculate the size of the structure, I unexpectedly forgot C language and sizeof this function, how long I did not write the program Ah!!! Ashamed, after the graduate students write embedded aspects of the program is particularly less, it seems to be often to practice practiced hand. Now reprint a see of the sizeof usage of the article, and share with you (the first technical class article is reproduced, ashamed).

#include"stdio.h"#include"string.h"#include"stdlib.h"intMain () { Short intSa=Ten; intA=Ten; LongLa=Ten; floatf = -; DoubleD= -; CharCh='C'; Charstr[]="ABC"; Char*p=str; structStr {DoubleD; Charch; intdata;    }STR_WU; structstr1{Charch; DoubleD; intdata;    }STR_WU1; printf ("sizeof (short):%d\n",sizeof(SA)); printf ("sizeof (int):%d\n",sizeof(a)); printf ("sizeof (LONG):%d\n",sizeof(LA)); printf ("sizeof (float):%d\n",sizeof(f)); printf ("sizeof (double):%d\n",sizeof(d)); printf ("sizeof (char):%d\n",sizeof(CH)); printf ("sizeof (String):%d\n",sizeof(str)); printf ("sizeof (point address):%d\n",sizeof(p)); printf ("sizeof (point):%d\n",sizeof(*p)); printf ("sizeof (Struct):%d\n",sizeof(STR_WU)); printf ("sizeof (Struct):%d\n",sizeof(STR_WU1)); System ("Pause");}

2. Floating-point data float,double,long double the top of the graph. 3. The pointer, in particular, distinguishes between pointers, what data the pointer points to, and the number of bytes it takes in memory. For example: the pointer points to a string, which is the length of the string, because one character occupies one byte in memory. If the pointer points to a data structure, the result should be the number of bytes of memory in the structured data. 4. struct type in the program above, the struct str{
Double D;
Char ch;
int data;
}STR_WU;
struct str1{
Char ch;
Double D;
int data;
}STR_WU1;
Two different structures, but the internal elements are the same, are Double,int,char, but the order is not the same, the result is different. Why At this time, because the VC storage data to its, the specific situation is as follows: type
Alignment (the offset at which the variable holds the starting address relative to the structure's start address) Char
The offset must be sizeof (char), which is a multiple of 1 int
The offset must be a multiple of sizeof (int), or 4, float
The offset must be sizeof (float), which is a multiple of 4 double
The offset must be sizeof (double) that is a multiple of 8 short
The offset must be sizeof (short) or a multiple of 2 such as: STR_WU, when allocating space for the above structure, VC allocates space for the first member D (type Double) based on the order and alignment of the member variables. The starting address is the same as the starting address of the structure (just 0 of the amount of sizeof (double), which occupies sizeof (double) = 8 bytes; Then allocates space for the second member CH (type char). At this point the next assignable address has an offset of 8 for the starting address of the struct, which is a multiple of sizeof (char), so the DDA is stored at an offset of 8 to satisfy the alignment, the member variable occupies sizeof (char) = 1 bytes; Then the third member data ( type int) allocates space, at which point the next assignable address has an offset of 9 for the starting address of the struct, not a multiple of the sizeof (int) =4, and in order to satisfy the constraint of the alignment to the offset, the VC automatically populates 3 bytes (the three bytes are not put anything). At this point the next assignable address has an offset of 12 for the starting address of the struct, which is just a multiple of the sizeof (int) =4, so the data is stored at an offset of 12, which occupies sizeof (int) = 4 bytes At this time, the entire structure of the member variable has been allocated space, the total space occupied by: 8+1+3+4=16, just the number of bytes of the structure (that is, the type of maximum space occupied by the structure of the number of bytes sizeof (double) =8), so no empty bytes need to be populated.   So the size of the whole structure is: sizeof (STR_WU) =8+1+3+4=16, which has 3 bytes is the VC automatically filled, did not put anything meaningful. And STR_WU1, the same reason: sizeof (char) = 1, while 1 is not a multiple of 8, thus increasing to 8,sizeof (double) = 8, now the start address is 16,16 is a multiple of sizeof (int), can be deposited. Thus the total number of addresses: sizeof (char) +7+sizeof (double) +sizeof (int) = 20, and 20 is not a multiple of 8 (sizeof (double) =8), so you need to add 4 addresses, that is, a total of 24.

  

The usage of sizeof in C language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.