473,385 Members | 1,647 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,385 software developers and data experts.

regarding sizeof operator for class with no data variables

consider this code snippet

why is the compiler forced to allocate 1 byte of memory for a class
which contains no data members.suppose i define more than 1 functions
inside this class ,why is the size of the class still 1 byte. What is
the reason for the allocation of 1 byte by the compiler for the class
A.

#include<iostream>
using namespace std;

class A
{
void func()
{ }
void func1()
{ }
void func2()
{ }
void func3()
{ }
};

int main()
{
cout<<sizeof(A)<<endl;
return 0;
}
Regards
Pratap

Aug 19 '07 #1
3 1781
On 2007-08-19 20:33, pratap wrote:
consider this code snippet

why is the compiler forced to allocate 1 byte of memory for a class
which contains no data members.suppose i define more than 1 functions
inside this class ,why is the size of the class still 1 byte. What is
the reason for the allocation of 1 byte by the compiler for the class
A.
The class does not have a size, an object of the class does. And the
reason is that each object must have an unique address, and how you you
have an unique address if you don't have a size? And the reason they
need an address is because that's how you refer to an object (in the end
when all fancy variable names have been removed by the compiler).

--
Erik Wikström
Aug 19 '07 #2
I think you should read "Inside the C++ Object Model" written by
Stanley B. Lippman, you will get what you want there.

Aug 20 '07 #3
On 2007-08-20 03:50, le**********@gmail.com wrote:
I think you should read "Inside the C++ Object Model" written by
Stanley B. Lippman, you will get what you want there.
Please quote the text you are replying to, if you don't it's hard to
determine your intentions. As an example, since you replied to my reply
to the op, your reply might be taken to mean that I was wrong and you
are correcting me.

--
Erik Wikström
Aug 20 '07 #4

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

Similar topics

5
by: chris | last post by:
What is a class ?? is it like a function ?? this has allways confused me as i am a newby to programming (since Basic in the 80's) thanks for any insight you can give
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...
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"; }
12
by: Casper | last post by:
How would one go about summing up the memmory custom tree structures occupies in memmory? Example: struct node { struct node *parent; unsigned int nChildCount; string folder; //string class...
3
by: Christof Warlich | last post by:
Hi, I'm trying to build an _efficient_, _purely_ _abstract_ API for inter process(or) communication, _completely_ hiding any implementation details. The core components are "Buffer", "Address"...
12
by: ozbear | last post by:
If one were writing a C interpreter, is there anything in the standard standard that requires the sizeof operator to yield the same value for two different variables of the same type? Let's...
12
by: sonu | last post by:
#include<stdio.h> main() { int x=10,y; y=sizeof(++x); printf("x=%d\ny=%d\n",x,y); } Oput Put
16
by: Joseph Paterson | last post by:
Hello, I've created a class to store 2 dimensional matrices, and I've been trying to overload the operator, so access to the elements of the matrix is easy. I have 3 private variables, _m, _row...
1
by: pratap | last post by:
consider this code snippet #include<iostream> using namespace std; class A { void func() { }
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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...

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.