Brook Julias
Brook Julias

Reputation: 2105

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

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.

#include <iostream>
using namespace std;

int main(){

    enum charRaces {DWARF,ELF,GNOME,HALFELF,HALFLING,HUMAN};
    enum classes{WARRIOR,FIGHTER,RANGER,PALADIN,WIZARD,MAGE,ILLUSIONIST,PRIEST,CLERIC,DRUID,ROGUE,THEIF,BARD};

    short int races[6] = {DWARF,ELF,GNOME,HALFELF,HALFLING,HUMAN};
    short int classes[13] = {WARRIOR,FIGHTER,RANGER,PALADIN,WIZARD,MAGE,ILLUSIONIST,PRIEST,CLERIC,DRUID,ROGUE,THEIF,BARD};

    cout << "sizeof(races)\t"  << sizeof(races) << endl;
    cout << "sizeof(classes)\t"  << sizeof(classes) << endl;

    system("pause");

    return(0);
}

Upvotes: 1

Views: 3366

Answers (6)

Anonymous Coward
Anonymous Coward

Reputation: 6226

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 *'

Upvotes: 0

Matt Kline
Matt Kline

Reputation: 10477

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.

Upvotes: 11

John Kugelman
John Kugelman

Reputation: 361565

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++.

Upvotes: 7

Greg Jandl
Greg Jandl

Reputation: 850

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

Upvotes: 0

Jatin Khurana
Jatin Khurana

Reputation: 1175

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.

Upvotes: 0

Collin
Collin

Reputation: 12277

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.

Upvotes: 1

Related Questions