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);
}
sizeofreturns the size of a variable (in this case, your arrays), wheresizeof(char)is 1. Since acharis one byte wide,sizeofreturns the size of the variable in bytes. Since eachshort intis 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.