Sizeof the secret of the PEN exam

Source: Internet
Author: User

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 ]:

 
 
  1. Write a macro to calculate the number of elements in the array.
  2.  
  3. # Define countof (arr )...
  4.  
  5. Example: int a [10], float B [20], char c [100]
  6.  
  7. Call:
  8. Countof (a), returns 10
  9. Countof (B), return value: 20
  10. 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?

 
 
  1. # Include <iostream>
  2.  
  3. Int getNum (void * pArr)
  4. {
  5. Return sizeof (pArr );
  6. }
  7.  
  8. Int _ tmain (int argc, _ TCHAR * argv [])
  9. {
  10. /** Note: When the sizeof operator acts on a static array, all storage bytes of the entire array are returned */
  11. Int myArr [10];
  12. Std: cout <"sizeof (int):" <sizeof (int) <std: endl;
  13. Std: cout <"myArr:" <sizeof (myArr) <std: endl;
  14.  
  15. /** Note: the sizeof operator cannot return the number of bytes of the dynamically allocated array */
  16. Int * pTest = new int (10 );
  17. Std: cout <"pTest:" <sizeof (pTest) <std: endl;
  18.  
  19. /** Test Description: Result of sizeof calculation parameter-number of bytes of pointer type */
  20. Int myParam [10];
  21. Char myChar [10];
  22. Std: cout <"getNum (myParam):" <getNum (myParam) <std: endl;
  23. Std: cout <"getNum (myChar):" <getNum (myChar) <std: endl;
  24.  
  25. Getchar ();
  26. Getchar ();
  27.  
  28. Return 0;
  29. }

Result ]:

 
 
  1. sizeof(int): 4  
  2. myArr : 40  
  3. pTest : 4  
  4. getNum(myParam) :4  
  5. 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

Related Article

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.