By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

strange problem with OOP

P: n/a
I wrote this program:

#include <iostream>
using namespace std;
class sample
{
private:
int age;
float salary;
char status;
};
int main()
{
sample s1;
cout << "sizeof int = " << sizeof(int) << endl;
cout << "sizeof float = " << sizeof(float) << endl;
cout << "sizeof char = " << sizeof(char) << endl;
cout << "sizeof sample class = " << sizeof(sample) << endl;
cout << "sizeof sample class object = " << sizeof(s1) << endl;
return 0;
}

Got this strange output:

sizeof int = 4
sizeof float = 4
sizeof char = 1
sizeof sample class = 12
sizeof sample class object = 12

Isn't it strange. I am using GCC 4.1.1. Isn't it should be 9 for the
last two one.
Please help.

Regards

Feb 24 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
* openbysource:
I wrote this program:

#include <iostream>
using namespace std;
class sample
{
private:
int age;
float salary;
char status;
};
int main()
{
sample s1;
cout << "sizeof int = " << sizeof(int) << endl;
cout << "sizeof float = " << sizeof(float) << endl;
cout << "sizeof char = " << sizeof(char) << endl;
cout << "sizeof sample class = " << sizeof(sample) << endl;
cout << "sizeof sample class object = " << sizeof(s1) << endl;
return 0;
}

Got this strange output:

sizeof int = 4
sizeof float = 4
sizeof char = 1
sizeof sample class = 12
sizeof sample class object = 12

Isn't it strange. I am using GCC 4.1.1. Isn't it should be 9 for the
last two one.
The compiler pads the struct with three extra bytes to make it a
multiple of four bytes large.

Depending on the machine and OS, this might improve the speed of
accessing such structs in an array, or it may be necessary to access
them at all.

With most compilers you can configure this.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 24 '07 #2

P: n/a
Thanks.

Feb 24 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.