Why is sizeof giving incorrect number of bytes in C++?

If you declare array a[4], it means it has 4 elements.
The only valid indicies are a[0], a[1], a[2], and a[3].

Accessing a[4] is invalid.
I don’t know why you think a[4] would have 5 elements in it.

This array has 4 elements, and its size is:

4-elements * 4-bytes = 16 bytes.

Leave a Comment