C # sizeof Analysis and example

Source: Internet
Author: User
Tags array length arrays

When a sizeof operates on a struct or variable, the return value is its actual size, which may include extra bytes inserted for byte alignment.
The second sentence: If the operation is a statically sized array, returns the size of the entire array
The third sentence: The sizeof operator cannot return the size of dynamically developed arrays or parameter groups.
If the operand is an array parameter in a function or a parameter of a function type, the sizeof gives the size of its pointer, so it is 4

int GetSize (char p[100]= "Hello")
{
return sizeof (p);
}
int main (int argc, char* argv[])
{
Char a[200]= "hello!";
Cout<<sizeof (a) <<endl<<getsize ("OK") <<endl;
return 0;
}<br> first returns 200, the second returns 4,SIZEOF (a) returns the size of the array, and the second is actually the size of the pointer variable, what's going on? <br> on the Internet to see the analysis of the strongman MSDN to know why:


sizeof is an operator under C/s + + that can be used for any variable name, type name, or constant value.
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;
Today's main discussion of pointers and arrays is specific to the MSDN
sizeof of pointer variable
The pointer records the address of another object. Since it is to hold the address, it 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 in bytes), and you can expect the sizeof result of the pointer variable to be 8 in a future 64-bit system.
sizeof of arrays
The sizeof value of an array equals the number of bytes of memory occupied by the array, that is, the length of the array
Now, just look at the snippet.
Char str[] = "Xxoo";
char *p = str;
int l = sizeof (str);
L = sizeof (p);
Can analyze
int l = sizeof (str);//sizeof the length of the array when the array name is computed
L = sizeof (p); P is a pointer, and the pointer is 4 bytes long on a 32-bit machine.
Here we can see that the array length is not the number of elements in the array
But what if we need to find the number of arrays? It's actually pretty simple. MSDN has models:
int xx = sizeof (OO)/sizeof (char); Total length/length of individual elements
sizeof () is an operator of the C + + language itself. Generally speaking, the behavior of operators belongs to the language itself, there is no question why. It is also said that C + + for the sizeof operator, the behavior is to return the array name of the byte count of the memory, and the other variables to return the type byte length,
Like what:
Put "Char str[];" Change to "long str[4];" Then "sizeof (STR);" The return value is 16 (16 byte). and pointing to the array str[4] pointer, the byte length of the memory address on the 32-bit platform is 4.
A classic example is quoted below:
double* (*a) [3][6];
Cout<<sizeof (a) <<endl; 4 A is a pointer
Cout<<sizeof (*a) <<endl; The *a is an array with 3*6 pointer elements
Cout<<sizeof (**a) <<endl; **a 6 pointers to one dimension of the array
Cout<<sizeof (***a) <<endl; 4 ***a is the first pointer to one dimension
Cout<<sizeof (****a) <<endl; 8 ****a is a double variable
Problem Resolution: A is a very strange definition, and he represents a pointer to an array of double*[3][6] types. Since it is a pointer, sizeof (a) is 4.
Since a is a pointer to a double*[3][6] type, *a represents a double*[3][6] multidimensional array type, so sizeof (*a) =3*6*sizeof (double*) = 72. Similarly, **a represents an array of double*[6] types, so sizeof (**a) =6*sizeof (double*) = 24. A means one of the elements, which is double*, so sizeof (***a) = 4. As for ****a, it's a double, so sizeof (****A) =sizeof (double) = 8.

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.