We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,437 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Object size concerns

Hi,
pls try to observe the size(i.e. total) of object for below program and in different cases as commented out.

#include<iostream>
using namespace std;
class B
{
int x; // try by commenting either data member and observe the object size 
int *p;
public:
virtual void show(){}
};

int main()
{
B o;
cout << sizeof(o)<< endl;
return 0;
}

Individually x contibute 4 byte and p contribute 8 byte but both together make object size to 16 byte, why it is.

Thanks
dipesh udhlani

2
Contributors
2
Replies
20 Hours
Discussion Span
8 Months Ago
Last Updated
3
Views
dpsh
Newbie Poster
2 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This link explains better

subith86
Junior Poster
127 posts since Mar 2011
Reputation Points: 30
Solved Threads: 14
Skill Endorsements: 1

Hi,
I found below link is good to understand
http://www.viva64.com/en/b/0004/

The made conclusion is that size of object is depends on, 1. order of data member(DM) declaration in class and 2. object size is always in multiple of size of it's maxm. size DM.

Thanks and Regards
dipesh udhlani

dpsh
Newbie Poster
2 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0656 seconds using 2.64MB