Can you use sizeof? (VC article)

Source: Internet
Author: User
Tags pack

Can you use sizeof? (VC article)

Source: http://dev.csdn.net/Develop/article/42/42446.shtm

This article mainly includes two parts, the first part focuses on VC, how to use sizeof to find the size of the structure, as well as easy to appear problems, and to solve the problem of the method, the second part of the VC sizeof the main usage.

1, sizeof application in the structure of the situation

See the following structure:

struct MYSTRUCT

{

Double dda1;

Char DDA;

int type

};

What will happen to the structure mystruct using sizeof? How much is sizeof (MYSTRUCT)? Perhaps you would ask:

sizeof (MyStruct) =sizeof (double) +sizeof (char) +sizeof (int) =13

But when you test the size of the above structure in VC, you will find that sizeof (MYSTRUCT) is 16. Do you know why the VC will come up with such a result.

In fact, this is a VC for variable storage of a special treatment. In order to improve the CPU storage speed, VC for some variables to start the address of the "alignment" processing. By default, VC stipulates that the offset of the starting address of each member variable in relation to the starting address of the structure must be a multiple of the number of bytes occupied by the type of the variable. The common types of alignment (vc6.0,32 bit systems) are listed below.

Type
Alignment (the offset at which the variable holds the starting address relative to the structure's starting address)

Char
Offset must be a multiple of sizeof (char) or 1

Int
Offset must be a multiple of sizeof (int), or 4

Float
Offset must be a multiple of sizeof (float) that is 4

Double
The offset must be sizeof (double), which is a multiple of 8

Short
Offset must be a multiple of sizeof (short), or 2

Each member variable at the time of storage according to the order in the structure of the application space, in accordance with the above alignment adjustment position, the blank byte VC will automatically fill. At the same time VC to ensure that the size of the structure of the structure of the number of bytes (that is, the structure of the maximum space occupied by the type of the number of bytes) multiples, so after the last member variable request space, will also automatically fill the blank bytes as needed.

The following examples to illustrate how the VC to store the structure.

struct MYSTRUCT

{

Double dda1;

Char DDA;

int type

};

When allocating space for the above structure, VC allocates space for the first member DDA1, based on the order and alignment of the member variable, and the starting address is the same as the starting address of the structure (just offset 0 is just a multiple of sizeof (double)), which occupies sizeof ( Double) = 8 bytes; Then allocate space for the second member, DDA, at which point the next available address is offset to the structure's starting address at 8, is a multiple of sizeof (char), so the DDA is stored in an offset of 8 where the alignment is satisfied. The member variable occupies sizeof (char) = 1 bytes; then allocates space for the third member type, at which point the next address that can be assigned has an offset of 9 for the starting address of the struct, not a multiple of the sizeof (int) =4, in order to satisfy the alignment constraint on the offset, VC automatically fills 3 bytes (these three bytes do not put anything), at this point, the next address that can be allocated is 12 of the offset of the structure's starting address, just a multiple of the sizeof (int) =4, so the type is stored in an offset of 12, which occupies the sizeof ( int) = 4 bytes, at which point the entire structure's member variable has been allocated space, the total space occupied is: 8+1+3+4=16, just for the structure of the number of bytes (that is, the structure of the maximum space occupied by the type of the number of bytes sizeof (double) =8) multiples, So there is no vacancy in bytes that need to be populated. So the size of the entire structure is: sizeof (mystruct) =8+1+3+4=16, of which 3 bytes are automatically filled with VC, not put any meaningful things.

Let's take another example and swap the position of the MYSTRUCT member variable above to make it the following:

struct MYSTRUCT

{

Char DDA;

Double dda1;

int type

};

How much space is occupied by this structure? In the VC6.0 environment, sizeof (MYSTRUC) can be obtained as 24. In combination with some of the principles of allocation space mentioned above, this paper analyzes how VC allocates space for the above structure. (Simple description)

struct MYSTRUCT

{

The Char dda;//offset is 0, the alignment is satisfied, and the DDA occupies 1 bytes;

Double dda1;//The offset of the next available address is 1, not sizeof (double) =8

Multiples of 7 bytes needed to be offset to 8 (to satisfy the alignment

way), so VC automatically fills 7 bytes, dda1 is stored at an offset of 8

Address, it occupies 8 bytes.

int type;//The offset of the next available address is 16, which is sizeof (int) =4 times

Number, to meet the alignment of int, so do not need VC automatic fill, type save

On an address with an offset of 16, it occupies 4 bytes.

};//all member variables are allocated space, the total size of the space is 1+7+8+4=20, not the structure

The number of section boundaries (that is, the number of bytes occupied by the type that occupies the maximum space in the structure sizeof

(double) =8), so you need to populate 4 bytes to meet the size of the structure

A multiple of the sizeof (double) =8.

So the total size of the structure is: sizeof (MYSTRUC) is 1+7+8+4+4=24. One of the total 7+4=11 byte is automatically filled with VC, did not put any meaningful things.

The special handling of the storage of the structure of VC does increase the speed of the CPU storage variable, but sometimes it also brings some trouble, we also shield off the default alignment of the variable, we can set the alignment of the variable.

VC provides the #pragma pack (n) to set the variable in n-byte alignment. N-byte alignment means that the offset of the starting address of the variable is stored in two ways: first, if n is greater than or equal to the number of bytes occupied by the variable, the offset must meet the default alignment, and secondly, if N is less than the number of bytes used for the variable's type, the offset is a multiple of n, not the default alignment. The total size of the structure also has a constraint, in the following two cases: if n is greater than the number of bytes occupied by all member variable types, the total size of the structure must be a multiple of the space occupied by the most space-consuming variable;

Otherwise, you must be a multiple of n. The following examples illustrate its use.

#pragma pack (push)//Save alignment status

#pragma pack (4)//set to 4-byte alignment

struct test

{

Char M1;

Double M4;

int m3;

};

#pragma pack (POP)//restore alignment status

The size of the above structure is 16, the following analysis of its storage, first for the M1 allocation of space, its offset is 0, to meet our own set alignment (4-byte alignment), M1 occupies 1 bytes. It then starts allocating space for the M4, with an offset of 1, which needs to complement 3 bytes, so that the offset is a multiple of n=4 (because sizeof (double) is greater than N), and M4 occupies 8 bytes. The space is then allocated for the M3, with an offset of 12, a multiple of 4, and a M3 of 4 bytes. Space has been allocated for all member variables and 16 bytes have been allocated, which satisfies a multiple of n. If the above #pragma pack (4) is changed to #pragma pack (16), then we can get the size of the structure to be 24. (Please let the reader analyze yourself)

2, sizeof Usage Summary

In VC, sizeof has a lot of usage, and it is easy to cause some mistakes. The following is a summary of the usage of sizeof based on the parameters behind sizeof.

A parameter is either a data type or a generic variable. such as sizeof (int), sizeof (long), and so on. It should be noted that the results of different system systems or different compilers may be different. For example, the int type occupies 2 bytes in a 16-bit system and occupies 4 bytes in a 32-bit system.

B The argument is an array or pointer. The following examples illustrate.

int A[50]; sizeof (a) =4*50=200; To find the size of the space the array occupies

int *a=new int[50];//sizeof (a) = 4; A is a pointer, sizeof (a) is a pointer

The size of the 32-bit system, of course, is accounted for 4 bytes.

C parameter is a struct or class. sizeof applications are identical in the handling of classes and structures. But there are two points to note that the first, struct, or static member in the class does not affect the structure or the size of the class, because the storage location of the static variable is independent of the structure or the instance address of the class.

Second, the size of a struct or class without a member variable is 1, because the structure or class must be guaranteed every

An instance has a unique address in memory.

The following examples illustrate that

Class test{int a;static Double c};//sizeof (Test) =4.

Test *s;//sizeof (s) =4,s is a pointer.

Class test1{};//sizeof (test1) = 1;

D parameter is other. The following examples illustrate.

int func (char s[5]);

{

cout<
The parameter of the number is processed as a pointer at the time of passing, the

In the case of sizeof (s), the size of the pointer is actually evaluated.

return 1;

}

sizeof (func ("1234")) =4//because the return type of func is int, the equivalent

Seek sizeof (int).

The above is the basic usage of sizeof, in the actual use should pay attention to analyze the allocation of VC variable strategy, such words can avoid some mistakes.

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.