c++ sizeof(array) return twice the array's declared length

前端 未结 6 1754
情书的邮戳
情书的邮戳 2020-12-02 02:47

I have a section of code in which two array are declared with sizes of 6 and 13, but when \'sizeof()\' is used the lengths are returned as 12 and 26.

#includ         


        
相关标签:
6条回答
  • 2020-12-02 03:17

    sizeof returns the actual memory in bytes used by the array. A fairly common idiom is to do something like this:

    short int races[6] = {DWARF,ELF,GNOME,HALFELF,HALFLING,HUMAN};
    size_t num_races = sizeof(races) / sizeof(races[0]);
    

    num_races would then have the number of elements in the array stored in it.

    0 讨论(0)
  • 2020-12-02 03:18

    The sizeof operator returns the size in bytes required to represent the type (at compile time).

    double array[10]; // type of array is: double[10]
    

    sizeof(array) has the same meaning as sizeof(double[10]), which is equal to:

    sizeof(double) * 10
    

    It's an array that can hold 10 double values. sizeof(array[0]) means: size of a single element in array, which is the same as sizeof(double) here. To get the actual number of elements, you have to divide the size of the array by the size of a single element:

    size_t num_elem = sizeof(array) / sizeof(array[0]);
    

    However, this doesn't work on pointers!

    double* p = array;
    

    sizeof(p) actually translates to sizeof(double*). Its size has nothing to do with the size of double or the size of the array it's pointing to. Instead, it's the size required to store the address to a memory location (32 bits on a 32bit operating). The information about the number of elements is lost!

    If you want to safely get the number of elements in an array, you can use this template:

    template<typename T, size_t N>
    size_t inline static_arrlen(T (&)[N]) {
        return N;
    }
    

    At compile-time, it deduces the type T and number of elements N, returning N.

    size_t num_elem = static_arrlen(array); // T=double, N=10
    

    If you're trying to get the array size from a pointer, it won't compile:

    static_arrlen(p); // ERROR: could not deduce template argument 
                      // for 'T (&)[N]' from 'double *'
    
    0 讨论(0)
  • 2020-12-02 03:24

    sizeof returns the size in bytes, which for an array is the number of items × the size of each item. To get the number of items divide by the size of one element.

    sizeof(races) / sizeof(races[0])
    

    Be careful with this. It will only work for arrays whose size is known at compile time. This will not work:

    void func(short int array[])
    {
        // DOES NOT WORK
        size_t size = sizeof(array) / sizeof(array[0]);
    }
    

    Here array is actually a short int * and sizeof(array) does not return the actual size of the array, which is unknown at compile time.

    This is one of many reasons to prefer std::vector or std::array to raw arrays in C++.

    0 讨论(0)
  • 2020-12-02 03:25

    sizeof returns the size of a variable (in this case, your arrays), where sizeof(char) is 1. Since a char is one byte wide, sizeof returns the size of the variable in bytes. Since each short int is two bytes wide on your system, an array of 6 of them will have size 12, and an array of 13 will have size 26.

    0 讨论(0)
  • 2020-12-02 03:31

    The sizeof operator is measured in units such that an unsigned char is 1 unit.

    On your platform, short is twice as large as char, thus the results you're seeing.

    To properly determine array length, you could use a macro such as:

    #define ARRAY_LEN(ary) (sizeof (ary) / sizeof (ary[0]))
    
    0 讨论(0)
  • 2020-12-02 03:44

    sizeof is an operator in C++ that measure the size in number of bytes.I think in your machine integer take 2 bytes that's why It's displaying the double the size of the array.

    0 讨论(0)
提交回复
热议问题