473,397 Members | 2,028 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,397 software developers and data experts.

sizeof object

Hi all,

class A
{
};

int main()
{
A a;
cout<<"size "<<sizeof(a)<<endl;
}

Now if i run this, i get sizeof(a) = 1, size is 1byte. Experts say that
even class A is not having any members, 1 byte is to differentiate b/w
objects.

Suppose if i add one int data member to class A i.e

class A
{
int a;
};

int main()
{
A a;
cout<<"size "<<sizeof(a)<<endl;
}

I supposed to get size = 4 + 1 = 5.. but its 4.. Why is it?? Here how
does it differentiate between objects..

Whats the theory behind this??

Cheers..

Aug 17 '05 #1
1 4157
Radde wrote:
class A
{
};

int main()
{
A a;
cout<<"size "<<sizeof(a)<<endl;
}

Now if i run this, i get sizeof(a) = 1, size is 1byte. Experts say that
even class A is not having any members, 1 byte is to differentiate b/w
objects.
Yes, and that's one of the significant differences between C++ and C,
BTW.

The need to differentiate comes from the ability to create an array of
empty objects. If their size were 0, all of them would have the same
address, right? So, there would be no way to tell between them...
Suppose if i add one int data member to class A i.e

class A
{
int a;
};

int main()
{
A a;
cout<<"size "<<sizeof(a)<<endl;
}

I supposed to get size = 4 + 1 = 5.. but its 4.. Why is it?? Here how
does it differentiate between objects..
If the size is 4, couldn't you already differentiate? There is no need to
add another byte for that, is there?
Whats the theory behind this??


No theory. Just practice.

V
Aug 17 '05 #2

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

Similar topics

3
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't...
4
by: Gopal-M | last post by:
I have the problem with sizeof operator I also want to implement a function that can return size of an object. My problem is as follows.. I have a Base class, say Base and there are many class...
2
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
2
by: Ronald A. Andersen | last post by:
********** header **************** struct articleFile2 { char CR; int intStampDate; }; *********** source *************** struct articleFile2 *ptrArticle2 = NULL;
31
by: Anjali M | last post by:
#include <stdio.h> int main() { int number = 10; printf("Number is: %d\n", number); printf("Sizeof of number is: %d\n", sizeof(number++)); printf("Number now is: %d\n", number);
3
by: Xiangliang Meng | last post by:
Hi, all. How does this 'sizeof(*area)' work? I think the reason is the sentence in C99: If the operand has type ¡®¡®pointer to type¡¯¡¯, the result has type ¡®¡®type¡¯¡¯, but I'm not sure. Could...
9
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have...
90
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
28
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it...
15
by: junky_fellow | last post by:
Hi, Is it possible to implement sizeof as a C function ?
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.