Contradictory results produced by sizeof

In a class, I have to define a constant member initialized in the constructor initializer list and an untagged enumeration to determine the size of an array. I am rather confused by the results produced by sizeof within the class and in the main function.

size= 8 _j = 32 (4*8? why)


The definition of 'sizeof' does not help to clarify the apparent contradiction. I would be most grateful to receive comments.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class MUntag21a
{
const double _reUN;
enum{size = 8};
public:
int _j, i[size];
MUntag21a(): _reUN(123.532123), _j(sizeof(i)){};
void Display21a(){
cout << "_reUN = " <<_reUN<< " size= " << size << "_j = "<< _j << endl;
}};

int main()
{
MUntag21a MUR;
int arr[1000];
int xxx = sizeof(arr[1000]);
cout << "xxx = "<<xxx<< endl;
}


gives:
_reUN = 123.532123 size= 8 _j = 32
xxx = 4
Hmm, the only time I see you use sizeof() is when you do sizeof(i) prints 32, which seems reasonable for the size of an int. What exactly is the problem?
size = 8 becouse it is a constant, _j = 32 becouse that is the size of eight integers, xxx = 4 becouse you only ceck the size of one array element
Many thanks to Zhuge and Hamsterman for their comments. Hamsterman's comment has clarified the defintion of sizeof.
Topic archived. No new replies allowed.