Strcut padding?

W

webinfinite

#include <iostream>
#include <string>

using namespace std;

int main() {
char t1;
struct t2{
char x;
};

cout << sizeof(t1) << " " << sizeof(t2) << endl;

typedef char& x1;
struct x2{
x1 x;
};

cout << sizeof(x1) << " " << sizeof(x2) << endl;
}

output:
1 1
1 4

Can anybody tell me why the size of x2 is 4? Why it's not 1?
 
E

EventHelix.com

#include <iostream>
#include <string>

using namespace std;

int main() {
  char t1;
  struct t2{
    char x;
  };

  cout << sizeof(t1) << "         " << sizeof(t2) << endl;

  typedef char& x1;
  struct x2{
    x1 x;
  };

  cout << sizeof(x1) << "         " << sizeof(x2) << endl;

}

output:
1         1
1         4

Can anybody tell me why the size of x2 is 4? Why it's not 1?

The following article should help:

http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top