Recently I made a pen test for a software company. One of the questions asked me to give an array name and find the number of elements in the array. At that time, I did not do it. Later I carefully thought about and analyzed it, I have made some new gains and shared them here ~~
Pen questions ]:
- Write a macro to calculate the number of elements in the array.
-
- # Define countof (arr )...
-
- Example: int a [10], float B [20], char c [100]
-
- Call:
- Countof (a), returns 10
- Countof (B), return value: 20
- Countof (c), returns 100
Answer: # define countof (arr) sizeof (arr)/sizeof (arr [0])
Minor issues ]:Do you know why the question requires a macro instead of a function similar to the following?
Int countof (void * arr)
{
}
If return sizeof (arr)/sizeof (arr [0]) is implemented in the above function, can we achieve the final expected result?
---------------------------------------------------------
Think about the following answer for a few minutes.
---------------------------------------------------------
Answer: it cannot be implemented because sizeof is the computation result during compilation and does not support computation results during runtime. Therefore, if sizeof (arr) is written into the function ), this sentence will only return the number of bytes occupied by the pointer type, that is, 4 bytes (32-bit operating system ).
Test exercise: Let's take a look at the output results of the following code?
- # Include <iostream>
-
- Int getNum (void * pArr)
- {
- Return sizeof (pArr );
- }
-
- Int _ tmain (int argc, _ TCHAR * argv [])
- {
- /** Note: When the sizeof operator acts on a static array, all storage bytes of the entire array are returned */
- Int myArr [10];
- Std: cout <"sizeof (int):" <sizeof (int) <std: endl;
- Std: cout <"myArr:" <sizeof (myArr) <std: endl;
-
- /** Note: the sizeof operator cannot return the number of bytes of the dynamically allocated array */
- Int * pTest = new int (10 );
- Std: cout <"pTest:" <sizeof (pTest) <std: endl;
-
- /** Test Description: Result of sizeof calculation parameter-number of bytes of pointer type */
- Int myParam [10];
- Char myChar [10];
- Std: cout <"getNum (myParam):" <getNum (myParam) <std: endl;
- Std: cout <"getNum (myChar):" <getNum (myChar) <std: endl;
-
- Getchar ();
- Getchar ();
-
- Return 0;
- }
Result ]:
- sizeof(int): 4
- myArr : 40
- pTest : 4
- getNum(myParam) :4
- getNum(myChar ) :4
Appendix ]:
The following is an explanation of sizeof on MSDN.
The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t.
The sizeof keyword specifies the number of bytes required to store input variables or types (including aggregation types. This keyword returns a size_t type variable.
When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment. when applied to a statically dimensioned array, sizeof returns the size of the entire array. the sizeof operator cannot return the size of dynamically allocated arrays or external arrays.
When applied to a structure type or variable, sizeof returns the actual size, which may include bytes filled for byte alignment. When used in a static dimension array, sizeof returns the size of the entire array. The sizeof operator cannot return the size of dynamically allocated arrays or external arrays.
This article from the "three shadows" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/318727