In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Sizeof operators and arrays
1. Who is sizeof?
When it comes to sizeof, the first thing you must know is that sizeof is not a function, but sizeof is an operator. The purpose of sizeof is to calculate the number of bytes of memory occupied by an object or type.
1.1 syntax of sizeof
Sizeof ():
That's why so many people in sizeof misunderstand it as a function. Inside the parentheses can be a data object or a data type. For this syntax with parentheses, you can ignore whether the parentheses are of a data type or a data object.
Sizeof object:
When I met you for the first time, I thought it was a syntax error, but in fact, this is also a use of sizeof to calculate the number of bytes of memory occupied by data objects. If you are calculating a data type, use the parentheses above. In this way, it makes sense that so many programmers use the syntax form of (), because it is universal.
1.2 how does sizeof calculate the size
If you have studied JAVA, you will find that the sizeof operator does not exist in JAVA at all. Is it because JAVA is not perfect? Actually this is not so. The reason why JAVA does not have a sizeof operator, as the JAVA designer said: JAVA does not need sizeof. The runtime of JAVA requires JVM, and different operating systems have corresponding JVM, which ensures the same size of all types in JVM. Whether you run on a 32-bit or 64-bit machine, the size of the data type is known, which is the most fundamental reason why JAVA portability can be realized. To get back to the topic, why does CCompact + need sizeof? Obviously, for a qualified C programmer, we should know that when the programs we write run on machines of different structures, the sizes of the types are different. When we need to know the size of a type to continue writing programs, we need sizeof. That's why we need sizeof.
Array is not that simple.
Arrays and pointers are important in the C language, so what are the data types of arrays? As described in other books, arrays are compound types (types combined with basic data types). An array is a collection of data of the same type, which is represented by a continuous string of memory in memory, and the size of memory is the product of the size of a single data type and the amount of data. In my personal opinion, arrays should be called data structures. The core of the implementation of the sequence table in the data structure is the array. Thus it can be seen that the array is more like a data structure predefined by the C language.
2.1 Syntax of arrays
Data type array name [number of data]
If we initialize it at the time of definition, the data size of the first dimension in the array can be left unwritten.
2.2 what exactly is the array name?
The array name is the name of a piece of memory space that points to it. When we talk about sizeof, then what sizeof (array name) calculates, of course, is the number of bytes of memory space that the array name points to, which is exactly the case. However, as we delve deeper into array names, we begin to wonder about sizeof (array names). Look at the following example:
Int arr [3] = {0jue 1pm 2}; printf ("% p", arr)
In the above example, the address of the first element of the array will be printed. From this we can speculate that arr is a pointer. We have a reason to speculate, because the pointer can store an address. But if we want to decide now, we need to consider sizeof (arr). At this point, you can answer what the size of sizeof (arr) is. There are two answers: 4 or 12. The answer is 4, we think of arr as a pointer, and the memory of the pointer takes up 4 bytes. 12, we think of the array name as a data structure with a size of 12 bytes. The correct answer is 12. At this point, our confusion is even deeper, since the compiler thinks that arr is not a pointer, but arr saves another address when printing an address. This is what we need to say: an array name is a data structure variable name similar to a pointer. Under normal circumstances, we can use the array name as a pointer. Let's move on to the following example:
Int arr [3] = {0meme1pr 2}; int arr2 [3] = {3pr 4pr 5}; arr= arr2
What we want to do in the above code is to assign the arr2 array to the arr array, but it is wrong when compiling, because arr is actually a constant pointer when it is represented as a pointer property. We will not assign a value again to a variable that has been initialized.
2.3 Array as function parameter
When the array is used as a function parameter, the array name is reduced to a pointer, and since it is a pointer, we can add, subtract and modify it. In the body of the function, the array name serves only as a pointer to an array. We know that it is a pointer, and as long as we figure out the type of data that the pointer actually points to, it is easy to use the pointer.
3. Sizeof and array
Take a look at the following code:
Int arr [2] [2] = {{0jue 1}, {2jre 3}}; printf ("% d,", sizeof (arr)); printf ("% d,", sizeof (arr [0])); printf ("% d\ n", sizeof (arr [0] [0])
What will the three printf print? Answer: 16, 8, 4.
3.1 sizeof (arr)
To find out the reason for the answer, let's first take a look at the multidimensional array.
A multidimensional array is actually an one-digit array in CAccord +. When a multidimensional array allocates memory, it allocates a complete and continuous memory space. This may not be well understood. Let's first take a look at the multidimensional array in JAVA, in terms of a two-dimensional array. When a two-dimensional array in JAVA allocates space, it first allocates an array of size 2, which holds the starting addresses of two one-dimensional arrays. These two one-dimensional arrays are not necessarily continuous in memory. If you look at the array in C, you can understand its complete contiguous meaning.
That's why sizeof (arr) prints 16. Because sizeof calculates the amount of memory space corresponding to the array name, regardless of the dimension size.
3.2 sizeof (arr [0])
Next let's take a look at sizeof (arr [0]). If there is no sizeof operator outside, arr [0], if viewed here as a pointer, operates as follows: (arr + 0), which still points to the address of the first element of the first line of the array. But under the sizeof operator, arr [0] obviously cannot be treated as a pointer and should be understood as an one-dimensional array of a two-dimensional array (logically, in fact, a multi-dimensional array is still an one-digit array). Arr [0] points to the one-digit array of the first row, we can understand that arr [0] is an array name, its memory space is the memory space corresponding to the first two elements of the arr array, we sizeof, the result should be 8.
3.2 sizeof (arr [0] [0])
Finally, looking at sizeof (arr [0] [0]), arr [0] [0] means that the first element of the one-dimensional array in the first row of the two-dimensional array is accessed, and its variable is of type int, so the result is 4.
From this point of view, when the array name is matched with the sizeof operator, it is not easy, and it still retains the feature of the array name as a data structure.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.