A
Alex Vinokur
classes that have virtual methods hold pointer to virtual table as
additional implicit data member. So, sizeof of such classes is sizeof
of all data (as struct-POD) plus 4.
It seems that use of casting for such classes is quite dangerous.
See sample below.
Why does reinterpret_cast permit such a casting?
=====================
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for
80x86
====== foo.cpp ======
#include <iostream>
using namespace std;
#define BUF_SIZE 8
#define SHOW(x) cout << #x << " = " << x << endl
struct Foo1
{
void foo() {} // non-virtual
char m_buf[BUF_SIZE];
void display(int in_start = 0)
{
cout << "Foo1 [" << in_start << ", " << (sizeof (m_buf) - 1) << "]
= ";
for (int i = in_start; i < static_cast<int>(sizeof (m_buf)); i++)
{
cout << m_buf;
}
cout << endl;
}
};
struct Foo2
{
virtual void foo() {} // virtual
char m_buf[BUF_SIZE];
void display(int in_start = 0)
{
cout << "Foo2 [" << in_start << ", " << (sizeof (m_buf) - 1) << "]
= ";
for (int i = in_start; i < static_cast<int>(sizeof (m_buf)); i++)
{
cout << m_buf;
}
cout << endl;
}
};
void display (char* in_p, int in_len, char* in_name)
{
cout << in_name << " (buf-size = " << in_len << ") : ";
for (int i = 0; i < in_len; i++)
{
cout << in_p;
}
cout << endl;
}
int main()
{
SHOW (sizeof (Foo1));
SHOW (sizeof (Foo2));
char buf1[BUF_SIZE];
char buf2[BUF_SIZE];
memcpy(buf1, "abcdxyz1", sizeof (buf1));
memcpy(buf2, "abcdxyz2", sizeof (buf2));
Foo1* p1 = reinterpret_cast<Foo1*>(buf1);
Foo2* p2 = reinterpret_cast<Foo2*>(buf2);
cout << endl;
p1->display();
p2->display();
p2->display(-4);
Foo1 foo1;
Foo2 foo2;
memcpy(foo1.m_buf, "qwertyu1", sizeof (foo1.m_buf));
memcpy(foo2.m_buf, "qwertyu2", sizeof (foo2.m_buf));
cout << endl;
foo1.display();
foo2.display();
char* pch1 = reinterpret_cast<char*>(&foo1);
char* pch2 = reinterpret_cast<char*>(&foo2);
cout << endl;
display (pch1, BUF_SIZE, "pch1");
display (pch2, BUF_SIZE, "pch2");
display (pch2, BUF_SIZE + 4, "pch2");
return 0;
}
=====================
====== Run ======
sizeof (Foo1) = 8
sizeof (Foo2) = 12
Foo1 [0, 7] = abcdxyz1
Foo2 [0, 7] = xyz2€A
Foo2 [-4, 7] = abcdxyz2€A
Foo1 [0, 7] = qwertyu1
Foo2 [0, 7] = qwertyu2
pch1 (buf-size = 8) : qwertyu1
pch2 (buf-size = 8) : ₪!A qwer
pch2 (buf-size = 12) : ₪!A qwertyu2
=================
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
additional implicit data member. So, sizeof of such classes is sizeof
of all data (as struct-POD) plus 4.
It seems that use of casting for such classes is quite dangerous.
See sample below.
Why does reinterpret_cast permit such a casting?
=====================
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for
80x86
====== foo.cpp ======
#include <iostream>
using namespace std;
#define BUF_SIZE 8
#define SHOW(x) cout << #x << " = " << x << endl
struct Foo1
{
void foo() {} // non-virtual
char m_buf[BUF_SIZE];
void display(int in_start = 0)
{
cout << "Foo1 [" << in_start << ", " << (sizeof (m_buf) - 1) << "]
= ";
for (int i = in_start; i < static_cast<int>(sizeof (m_buf)); i++)
{
cout << m_buf;
}
cout << endl;
}
};
struct Foo2
{
virtual void foo() {} // virtual
char m_buf[BUF_SIZE];
void display(int in_start = 0)
{
cout << "Foo2 [" << in_start << ", " << (sizeof (m_buf) - 1) << "]
= ";
for (int i = in_start; i < static_cast<int>(sizeof (m_buf)); i++)
{
cout << m_buf;
}
cout << endl;
}
};
void display (char* in_p, int in_len, char* in_name)
{
cout << in_name << " (buf-size = " << in_len << ") : ";
for (int i = 0; i < in_len; i++)
{
cout << in_p;
}
cout << endl;
}
int main()
{
SHOW (sizeof (Foo1));
SHOW (sizeof (Foo2));
char buf1[BUF_SIZE];
char buf2[BUF_SIZE];
memcpy(buf1, "abcdxyz1", sizeof (buf1));
memcpy(buf2, "abcdxyz2", sizeof (buf2));
Foo1* p1 = reinterpret_cast<Foo1*>(buf1);
Foo2* p2 = reinterpret_cast<Foo2*>(buf2);
cout << endl;
p1->display();
p2->display();
p2->display(-4);
Foo1 foo1;
Foo2 foo2;
memcpy(foo1.m_buf, "qwertyu1", sizeof (foo1.m_buf));
memcpy(foo2.m_buf, "qwertyu2", sizeof (foo2.m_buf));
cout << endl;
foo1.display();
foo2.display();
char* pch1 = reinterpret_cast<char*>(&foo1);
char* pch2 = reinterpret_cast<char*>(&foo2);
cout << endl;
display (pch1, BUF_SIZE, "pch1");
display (pch2, BUF_SIZE, "pch2");
display (pch2, BUF_SIZE + 4, "pch2");
return 0;
}
=====================
====== Run ======
sizeof (Foo1) = 8
sizeof (Foo2) = 12
Foo1 [0, 7] = abcdxyz1
Foo2 [0, 7] = xyz2€A
Foo2 [-4, 7] = abcdxyz2€A
Foo1 [0, 7] = qwertyu1
Foo2 [0, 7] = qwertyu2
pch1 (buf-size = 8) : qwertyu1
pch2 (buf-size = 8) : ₪!A qwer
pch2 (buf-size = 12) : ₪!A qwertyu2
=================
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn