How To Determine The Size Of An Array

Table of contents:

How To Determine The Size Of An Array
How To Determine The Size Of An Array

Video: How To Determine The Size Of An Array

Video: How To Determine The Size Of An Array
Video: How to determine or get array length (size) in C/C+ 2024, November
Anonim

Arrays are one of the most frequently used forms of data storage in the course of program operation. They allow you to organize elements of the same type in an ordered sequence and get quick access to them by index. Very often, when developing applications in powerful and flexible programming languages that allow direct memory access, such as C ++, you need to determine the size of an array.

How to determine the size of an array
How to determine the size of an array

Necessary

C ++ compiler

Instructions

Step 1

Determine the size of the array at compile time by calculating it using the sizeof operator. This operator returns the amount of memory (in bytes) occupied by the argument passed to it. The argument can be either a variable or a type identifier. The sizeof operator returns the finite amount of memory occupied by the object at the stage of program execution (taking into account, for example, the settings for the alignment of the structure fields), but it is calculated at the compilation stage.

Step 2

To determine the size of an array using the sizeof operator, divide its entire size by the size of one element. For example, if you have the following definition of an array: int aTemp = {10, 20, 0xFFFF, -1, 16}, then its size can be calculated as: int nSize = sizeof (aTemp) / sizeof (aTemp [0]);

Step 3

For a more convenient application of this method, it makes sense to define a macro: #define countof (a) (sizeof (a) / sizeof (a [0])) Note that since the value of the sizeof operator is calculated at compile time, at the point, where the calculation is performed, information about the volume of the array and its elements must be available explicitly. In other words, it is impossible to determine the parameters of an array of unknown size by its extern declaration.

Step 4

Determine the size of the array during the execution of the program, using the known sign of its termination. One of the approaches that allows storing and transmitting data in the form of arrays of indefinite length is to allocate a special value for a sign indicating the completion of a data sequence. For example, single-byte C-style strings that are character arrays must end with a value of 0, packed variable-length C-string arrays are zero-terminated, and pointer arrays must be null-terminated.

Step 5

To determine the size of an array represented in this way, scan it element by element until you find the terminating element. Increment the zero-initialized counter during the scan. Or, increase the value of the pointer to an array element, and after scanning, calculate the difference between the pointers to the current and first elements.

Step 6

Get the size of a dynamic array represented by a framework or library object by calling its method. Any classes that encapsulate the functionality of such arrays have methods for getting the current number of elements. For example, the template class std:: vector of the C ++ standard library has a size method, the QVector class of the Qt framework has a count method, and the analogous CArray class of the MFC framework has a GetCount method.

Recommended: