0
#include <iostream>
#include <cstring>

using namespace std;

class Person {
    char name[10];
    int age;
};

int main(void) {
    const size_t size = sizeof(Person);
    cout << size << endl;
    system("PAUSE");
    return 0;
}

Output is: 16 and that's ok, because 10 bytes (char[10]) is rounded to 16 by Visual Studio 2015 compiler. If I comment out the int age; line, then the output is 10(is it 2+8 from rounding values?) Why not still 16 if compiler rounds values to 2,4,8 and 16 ?

Why compiler is not consistent and doesn't print 14 for both char name[10]; and int age;. And 10 for only char name[10]; (when I comment out age)

Jagoda Gorus
  • 329
  • 4
  • 18

0 Answers0