This is a value that relies on the compilation system, a
As defined as the typedef unsigned int size_t; compilers are numerous, but as a specification, they guarantee that char, signed
The sizeof value of char and unsigned char is 1, after all, char is the smallest data type that can be programmed.
The explanation on MSDN is:
The sizeof keyword gives the amount of storage, in bytes, associated with avariable or a
Type (including aggregate types). This keyword returns a value of type
size_t.
2. Grammar:
There are three grammatical forms of sizeof, as follows:
1) sizeof (object); sizeof (object);
2) sizeof (TYPE_NAME); sizeof (type);
3) sizeof object; sizeof object;
So three kinds of sizeof are all right.
Copy Code code as follows:
#include <stdio.h>
Main ()
{
int b;
printf ("%d\n", sizeof B);
printf ("%d\n", sizeof (b));
printf ("%d\n", sizeof (int));
}
4. SizeOf of basic data types
The basic data types here refer to simple built-in data types such as short, int, long, float, and double, because they are both
, so it may be different to take the values under different systems, which must arouse our attention and try not to
This has caused trouble for the transplant of your own program. In general, in a 32-bit compilation environment, the value of sizeof (int) is 4.
5. sizeof of pointer variable
is equal to the width of the address bus select、read inside the computer. So in a 32-bit computer, the return value of a pointer variable must be 4 (note that the result is
Bytes, you can expect the sizeof result of the pointer variable to be 8 in a future 64-bit system.
The sizeof value of a pointer variable has nothing to do with the object that the pointer refers to, because all pointer variables have equal memory size, so
MFC message handlers use two parameters wparam, lparam to deliver a variety of complex message structures (using
Pointer to the structure body).
6. SizeOf of array
The sizeof value of an array equals the number of bytes of memory occupied by the array, such as:
Char a1[] = "ABC";
int a2[3];
sizeof (A1); The result is 4, and a null terminator exists at the end of the character.
sizeof (A2); Result is 3*4=12 (dependent on int)
sizeof as the number of elements of the array is not correct, to find the number of elements of the array has the following two kinds of writing: int c1 = sizeof (A1)
/sizeof (char); Total length/length of individual elements
int c2 = sizeof (A1)/sizeof (a1[0)); Total length/length of the first element. Note that array names do function parameter passing
is degraded to a pointer.
7. SizeOf of the structural body
struct S1
{
char c;
int i;
};
The result of the sizeof equals the number of bytes of memory in the object or type, so let's look at the S1 memory allocation: S1 S1
= {' A ', 0xFFFFFFFF};s1 address is 0x0012ff78, its data content is as follows:
3-byte cc in the middle of 0012ff78:61 CC CC FF FF FF FF see instructions on MSDN: when applied to a
Structure type or variable, sizeof returns the actual size, which may
Include padding bytes inserted for alignment.
This is the byte alignment! Why do I need byte alignment the principle of computer composition teaches us that this helps to speed up the number of computers, otherwise
It's going to take a lot of instruction cycles. To do this, the compiler handles the structure by default (in fact, the number of other places
The same is true for variables, so that the basic data type (short, etc.) with a width of 2 is located on an address that is divisible by 2, allowing for a base width of 4
data types (int, etc.) are located on an address that is divisible by 4, and so on. In this way, the middle of a two number may
You need to add padding bytes, so the sizeof value of the entire structure increases.
1.sizeof is the operator , which is essentially the same as the nature of the subtraction, executed at compile time, not at runtime.
So what if this is validated in programming?
Copy Code code as follows:
<span style= "font-size:18px" > #include <iostream></span><span style= "font-size:14px" >
using namespace Std;
int main ()
{
int i=1;
cout<<i<<endl;
sizeof (++I);
cout<<i<<endl;
return 1;
}</span>
Enter a result of 1
1
The side effects of the ++i in the sizeof are not shown, because only one may be, at compile time sizeof execution will be ++i processed, and ++i side effects are eliminated. If the sizeof is carried out at run time, be sure to pay attention to ++i. In fact, the implementation of sizeof should be done with macros, and macros are executed at compile time. Specific implementation can refer to the following.
2.sizeof (' a ') in C language result is 4, in C + + results are 1, read an article said C sizeof focus on "number", and C + + sizeof more emphasis on "character."
3. The article is about two classic applications that use macros to realize sizeof.
Copy Code code as follows:
Applies to non-array
#define _SIZEOF (T) (size_t) ((t*) 0 + 1))
Applies to arrays
#define ARRAY_SIZEOF (T) (size_t) (&t+1)-(size_t) (&t))
First, two small examples illustrate the application of two macros, for the first such as _sizeof (int); The result is 4; for the second, declare an array int a[4 with a size of 4, and array_sizeof (a) The result is 16.
For a non-array macro definition, first converts 0 to the address that the pointer to the t* type points to (at this point the address is 0). Then add 1 to the address of type T, which is equivalent to the size of the T type (that is, the size of the non-array t). The previous size_t only returns an integer that converts the address to an int type.
A simple example: int* p; p=p+1; --------P is a pointer to a int* type, p+1 the equivalent of 4 bytes in the address space.
For a macro definition of an array, similar to a non-array macro definition, for ease of understanding, array T can be considered a user-defined type, &t a pointer to an array type, and an array type pointer plus 1 equals the size of the array on the address. Because it is a user-defined type, you cannot force 0 to be converted to an array type of address, and you can only subtract the previous address with an additional 1 address, and the difference is the size of the byte that the array itself occupies.