473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

strange problem with OOP

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
2 1391
* 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
Thanks.

Feb 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Arthur | last post by:
I've come across some strange xml, that I need to deal with, it looks like this:- <root> <foo attr="1">Some random strange text. <bar attr="2">blar</bar> <bar attr="3">blar blar</bar> <bar...
2
by: Olaf | last post by:
I have a frameset page witch contains the myFuc() function. The function is accessed from a page in one of the frames in the frameset. An example is shown below. <input...
7
by: M O J O | last post by:
Hi, I'm developing a asp.net application and ran into a strange css problem. I want all my links to have a dashed underline and when they are hovered, it must change to a solid line. Sounds...
25
by: Neil Ginsberg | last post by:
I have a strange situation with my Access 2000 database. I have code in the database which has worked fine for years, and now all of a sudden doesn't work fine on one or two of my client's...
0
by: unknown | last post by:
Hi, I am developing an online book store with shopping cart. My shopping cart is represented as a Xml server control and I am using an XSLT to render it at the client side. I am using an...
0
by: Kris Vanherck | last post by:
yesterday i started getting this strange error when i try to run my asp.net project: Compiler Error Message: CS0006: Metadata file 'c:\winnt\microsoft.net\framework\v1.1.4322\temporary asp.net...
6
by: Gary | last post by:
I have an application that has been working just fine for a couple of years. It queries a SQL database and returns some formatted data back to the client. I have a new client, who has a larger...
11
by: Martin Joergensen | last post by:
Hi, I've encountered a really, *really*, REALLY strange error :-) I have a for-loop and after 8 runs I get strange results...... I mean: A really strange result.... I'm calculating...
20
by: SpreadTooThin | last post by:
I have a list and I need to do a custom sort on it... for example: a = #Although not necessarily in order def cmp(i,j): #to be defined in this thread. a.sort(cmp) print a
14
by: blumen | last post by:
Hi all, I'm a newbie in VB.Net Programming.. Hope that some of you can help me to solve this.. I'm working out to read,parse and save textfile into SQL Server. The textfile contains thousands...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.