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

sizeof and a class with virtual members

The code is:
-----------------------------------------------
#include <cstdio>

#define S(X) printf(#X ": %d\n", sizeof(X));

class Seekable
{
public:
virtual void seek(int to, char from);
virtual int tell();
virtual int size();
};
int main() {
S(Seekable);
return 0;
}
-----------------------------------------------
The output is:
-----------------------------------------------
Seekable: 4
-----------------------------------------------

How can be possible?
Compiler is Mingw.
Dec 24 '07 #1
5 1365
Chameleon wrote:
The code is:
-----------------------------------------------
#include <cstdio>

#define S(X) printf(#X ": %d\n", sizeof(X));

class Seekable
{
public:
virtual void seek(int to, char from);
virtual int tell();
virtual int size();
};
int main() {
S(Seekable);
return 0;
}
-----------------------------------------------
The output is:
-----------------------------------------------
Seekable: 4
-----------------------------------------------

How can be possible?
Compiler is Mingw.
virtual function is often implemented with v-table.
compiler inserts a v-ptr into each instance of a class with at least one
virtual function.
sizeof (v-ptr) == 4, like as ordinary pointer.
Dec 24 '07 #2
O/H Barry έγραψε:
Chameleon wrote:
>The code is:
-----------------------------------------------
#include <cstdio>

#define S(X) printf(#X ": %d\n", sizeof(X));

class Seekable
{
public:
virtual void seek(int to, char from);
virtual int tell();
virtual int size();
};
int main() {
S(Seekable);
return 0;
}
-----------------------------------------------
The output is:
-----------------------------------------------
Seekable: 4
-----------------------------------------------

How can be possible?
Compiler is Mingw.

virtual function is often implemented with v-table.
compiler inserts a v-ptr into each instance of a class with at least one
virtual function.
sizeof (v-ptr) == 4, like as ordinary pointer.

Holy shit!
Until now, I believe whole v-table is part of class data!
It is clear now!
v-table is static data for each class declaration.
The objects have only the pointer to these static data (v-table). Not
the whole v-table.
Thanks
Dec 24 '07 #3
Chameleon wrote:
The code is:
-----------------------------------------------
#include <cstdio>

#define S(X) printf(#X ": %d\n", sizeof(X));
You received your answer, but I'll point that the above is invalid. The
sizeof operator gives a result that is type size_t, which is an
unsigned integral type. The %d is for signed ints. size_t may or may
not even be an int.

#define S(X) printf(#X ": %d\n", (int)sizeof(X));

would work, or some variation. I didn't bother with a c++ cast, but you
could use one as well.

Brian
Dec 24 '07 #4
On 2007-12-24 12:28:32 -0600, "Default User" <de***********@yahoo.comsaid:
Chameleon wrote:
>The code is:
-----------------------------------------------
#include <cstdio>

#define S(X) printf(#X ": %d\n", sizeof(X));

You received your answer, but I'll point that the above is invalid. The
sizeof operator gives a result that is type size_t, which is an
unsigned integral type. T
Or better yet,

std::cout << "Size: " << sizeof(X) << std::endl;

Dec 24 '07 #5
On Dec 24, 11:03 am, Chameleon <cham_...@hotmail.comwrote:
The code is:
-----------------------------------------------
#include <cstdio>

#define S(X) printf(#X ": %d\n",sizeof(X));
This macro isn't safe for general use. The following example would be
very likely to cause problems:

int c = 5, d = 3;

S(c%d);

You should prefer:

#define S(X) printf("%s: %d\n", #X, static_cast<int>(sizeof(X)));

....which also reflects the comment elsewhere in this thread that
sizeof returns an unsigned type.
Dec 27 '07 #6

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...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
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"; }
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"...
32
by: moleskyca1 | last post by:
This may be stupid question, but why is sizeof(Base) == 1 in: int main(int argc, char* argv) { class Base { }; cout << sizeof(Base) << endl; return 0; }
2
by: GRenard | last post by:
Hello, We just switch in our company to VisualStudio 2005 and the new ATL library. We use a wrapper to use CFileDialog. Its name is CFileDialogDeluxe Here the call CFileDialogDeluxe...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
5
Banfa
by: Banfa | last post by:
I thought I would write a little article about this because it is regularly asked as a question in the Forums. Firstly this is a C++ issue, it is strictly against the rules of C to create a class...
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
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.