Sizeof operator I

Source: Internet
Author: User

The basis for testing is Windows 32-bit platform. VS2008 development tool: Test Basic type 1. the basic rule of the sizeof operator is to return the number of memory bytes occupied by an object or type name. The type of returned value is size_t, And the length is measured in bytes. It is determined during compilation rather than runtime. • The sizeof operation on the reference type will return the internal space required to store the object of this reference type. • The sizeof operation on the pointer will return the internal size required to store the pointer. Note that if you want to obtain the size of the object pointed to by the pointer, you must reference the pointer. Because sizeof returns the storage length of the entire array in the memory, you can use the result of sizeof array to divide it by the result of sizeof its element type to obtain the number of array elements: for example, int ia [8]; int sz = sizeof (ia)/sizeof (* ia); cout <sz <endl; // 8 2. what is the difference and connection between sizeof and strlen? (2.1) The result type of the sizeof operator is size_t, which indicates that typedef is of the unsigned int type in the header file. This type ensures that it can accommodate the maximum size of bytes of the created object. (2.2) sizeof is an operator and strlen is a function. If sizeof is a type, you must add an arc. If it is a variable name, you can do not add an arc. (2.3) when the array name is used as the function parameter, It is degraded to a pointer. When the array name is used as the sizeof () parameter, the array name is not degraded because sizeof is not a function. (2.4) sizeof can be used as a parameter. strlen can only use char * as a parameter and must end with "\ 0. (2.5) Most compilation programs calculate sizeof during compilation, which is why sizeof (x) can be used to define the array dimension. The strlen result can be calculated only when running. It is used to calculate the actual length of the string, not the memory size occupied by the type.

 

(2.6) is used as a structure type or variable. sizeof returns the actual size. If sizeof is used as an array, it can only measure the size of a static array and cannot detect the size of dynamically allocated or external arrays. // Handle static Arrays: char str [11] = "0123456789"; // note that the str size should be equal to '\ 0, otherwise, the compiler reports the error cout <strlen (str) <endl; // 10 strlen calculates the length of the string and ends with the end character x00 as the string. Cout <sizeof (str) <endl; // 11 sizeof calculates the memory space occupied by the allocated array str [11], which is not changed by the content stored in it. (2.7) When an array is passed as a parameter to a function, the pointer instead of an array is passed, and the first address of the array is passed, for example, fun (char [8]) fun (char []) it is equivalent to fun (char *) void Func (char str [100]) {cout <sizeof (str) <endl;} void * p = malloc (100 ); cout <sizeof (p) <endl; therefore, both sizeof (str) and sizeof (p) are 4. In C ++, passing an array by parameters is always a pointer to the first element of the array. The Compiler does not know the size of the array. If you want to know the size of the array in the function, you need to do this: after entering the function, copy it with memcpy. The length is transmitted by a form parameter. Void fun (unsigned char * p1, int len) {unsigned char * buf = new unsigned char [len + 1]; memcpy (buf, p1, len );} // pointer processing: char * ss = "0123456789"; cout <sizeof (ss) <endl; // 4 ss is a character pointer to a String constant, sizeof obtains the space occupied by a pointer. It should be a long integer cout <sizeof (* ss) <endl; // 1 * the first character of ss is actually the memory space occupied by the first ''of the string. It is of the char type and occupies the cout <strlen (ss) <endl; // 10. To obtain the length of this string, use strlen, the following method can be used to determine the number of elements that the static array can accommodate: int a [3] = {1, 2, 3}; cout <sizeof a/sizeof (typeid (a [0]). name (); // 3 (2.8) sizeof () and initial non-initialization. It does not matter. strlen () is related to initialization. Comparison: 3. sizeof struct of simple struct each variable occupies space separately. The details of byte alignment are related to the compiler implementation, but generally the three criteria are met: 1) the first address of the struct variable can be divisible by the size of its widest basic type member; 2) the offset of each member of the struct to the first address of the struct is an integer multiple of the member size. If necessary, the compiler will add the padding byte (internal adding) between the members. 3) the total size of the struct is an integer multiple of the size of the widest basic type of the struct. If necessary, the compiler will add the trailing padding after the last member ). Depending on the memory: The Byte alignment requires three bytes of CC in the middle. The pack command can be used to adjust the structure alignment mode. # The basic usage of pragmapack is: # pragmapack (n), and n is the number of bytes alignment, the values are 1, 2, 4, 8, and 16. The size of the "null struct" (excluding data members) is not 0, but 1. The "null struct" variable must also be stored. 4. the sizeof union variable public space of the union. Resolution: The maximum variable type is int [5], which occupies 20 bytes. Therefore, the variable size is 20.

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.