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

Class layout

Hi,

the following code is the boiled-down version of something that
gives us trouble on GCC. In the real code, 'F' is some fixed-size
array class, 'Q' and 'V' are derived classes that probably aren't
very interesting for this question, 'B' is a 4x4 matrix base and
'M' is the matrix class in use.
The code works with VC and several versions of GCC, but in one
test using GCC it breaks with nonsensical values for the 2nd-4th
row of the matrix. The trouble is, it only does so when no
additional statements are added to the function (that's mimicked
here in 'main()') and not, if the code is run under a debugger.
The problem doesn't appear with this boiled-down example code, but
looking at the code and the symptoms, I have the suspicion that
this invokes UB anyway and would like the group's opinion whether
my suspicion is right.
'M<T>::data()'assumes that all of data members of the inherited
'F's are laid out contiguously. Can we assume this regarding to
the standard? My suspicion is, we can't. But I don't know why we
either can and can't. I believe struct layout in C is rather
strict and reliable, but I don't know what's needed of the C++-
only features (inheritance, templates etc.) is needed to make
C++ deviate from that.

TIA,

Schobi
--8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<--

#include <iostream>
#include <string>

template< typename T >
struct F {
T val[4];
F() {val[0]=T();val[1]=T();val[2]=T();val[3]=T();}
F(T a0, T a1, T a2, T a3) {val[0]=a0 ;val[1]=a1 ;val[2]=a2 ;val[3]=a3 ;}
T& operator[](std::size_t i) {return val[i];}
const T& operator[](std::size_t i) const {return val[i];}
T* begin() {return &val[0];}
const T* begin() const {return &val[0];}
T* end () {return &val[4];}
const T* end () const {return &val[4];}
};

template< typename T >
struct Q : public F<T{
Q() : F() {}
Q(T a0, T a1, T a2, T a3) : F(a0,a1,a2,a3) {}
};

template< typename T >
struct V : public Q<T{
V() : Q() {}
V(T a0, T a1, T a2, T a3) : Q(a0,a1,a2,a3) {}
};

template< typename T >
struct B : public V< V<T {
typedef V<TRow;

B() {assign( 0, 0, 0, 0
, 0, 0, 0, 0
, 0, 0, 0, 0
, 0, 0, 0, 0 );}

B( T a00, T a01, T a02, T a03
, T a10, T a11, T a12, T a13
, T a20, T a21, T a22, T a23
, T a30, T a31, T a32, T a33 ) {assign( a00, a01, a02, a03
, a10, a11, a12, a13
, a20, a21, a22, a23
, a30, a31, a32, a33 );}

void assign( T a00, T a01, T a02, T a03
, T a10, T a11, T a12, T a13
, T a20, T a21, T a22, T a23
, T a30, T a31, T a32, T a33 )
{
this->val[0][0]=a00;this->val[0][1]=a01;this->val[0][2]=a02;this->val[0][3]=a03;
this->val[1][0]=a10;this->val[1][1]=a11;this->val[1][2]=a12;this->val[1][3]=a13;
this->val[2][0]=a20;this->val[2][1]=a21;this->val[2][2]=a22;this->val[2][3]=a23;
this->val[3][0]=a30;this->val[3][1]=a31;this->val[3][2]=a32;this->val[3][3]=a33;
}

};

template< typename T >
struct M : public B<T{
M() : B<T>(), i() {}
const T* data() const {return B<T>::begin()->begin();}
int i;
};

int main()
{
M<floatmatrix;
matrix.assign(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,1 5);
const float* data = matrix.data();

for( std::size_t i = 0; i < 16; ++i ) {
std::cout << data[i] << '\n';
}

return 0;
}
Nov 10 '08 #1
2 1580
Hendrik Schober wrote:
Hi,

the following code is the boiled-down version of something that
gives us trouble on GCC. In the real code, 'F' is some fixed-size
array class, 'Q' and 'V' are derived classes that probably aren't
very interesting for this question, 'B' is a 4x4 matrix base and
'M' is the matrix class in use.
The code works with VC and several versions of GCC, but in one
test using GCC it breaks with nonsensical values for the 2nd-4th
row of the matrix. The trouble is, it only does so when no
additional statements are added to the function (that's mimicked
here in 'main()') and not, if the code is run under a debugger.
The problem doesn't appear with this boiled-down example code, but
looking at the code and the symptoms, I have the suspicion that
this invokes UB anyway and would like the group's opinion whether
my suspicion is right.
'M<T>::data()'assumes that all of data members of the inherited
'F's are laid out contiguously. Can we assume this regarding to
the standard?
No. Who knows what kind of padding your compiler sticks in your
'F' class right after the 'val'...
My suspicion is, we can't. But I don't know why we
either can and can't. I believe struct layout in C is rather
strict and reliable, but I don't know what's needed of the C++-
only features (inheritance, templates etc.) is needed to make
C++ deviate from that.

TIA,

Schobi
--8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<--

#include <iostream>
#include <string>

template< typename T >
struct F {
T val[4];
F()
{val[0]=T();val[1]=T();val[2]=T();val[3]=T();} F(T a0, T a1, T
a2, T a3) {val[0]=a0 ;val[1]=a1
;val[2]=a2 ;val[3]=a3 ;} T& operator[](std::size_t i) {return
val[i];} const T& operator[](std::size_t i) const {return val[i];} T*
begin() {return
&val[0];} const T* begin() const {return
&val[0];} T* end () {return
&val[4];} const T* end () const {return
&val[4];} };
Your use of 'flowed' format is apparent here... :-)

But let me just say, ugh! Why couldn't you initialise the 'val' at
least in the default constructor? You could do

F() : val() {}

right?
>
template< typename T >
struct Q : public F<T{
Q() : F() {}
Q(T a0, T a1, T a2, T a3) : F(a0,a1,a2,a3) {}
};

template< typename T >
struct V : public Q<T{
V() : Q() {}
V(T a0, T a1, T a2, T a3) : Q(a0,a1,a2,a3) {}
};

template< typename T >
struct B : public V< V<T {
typedef V<TRow;
I don't think you're using this typedef. Are you?
>
B() {assign( 0, 0, 0, 0
, 0, 0, 0, 0
, 0, 0, 0, 0
, 0, 0, 0, 0
);}
B( T a00, T a01, T a02, T a03
, T a10, T a11, T a12, T a13
, T a20, T a21, T a22, T a23
, T a30, T a31, T a32, T a33 ) {assign( a00, a01,
a02, a03 ,
a10, a11, a12,
a13 , a20,
a21, a22, a23 , a30, a31, a32, a33 );}
void assign( T a00, T a01, T a02, T a03
, T a10, T a11, T a12, T a13
, T a20, T a21, T a22, T a23
, T a30, T a31, T a32, T a33 )
{

this->val[0][0]=a00;this->val[0][1]=a01;this->val[0][2]=a02;this->val[0][3]=a03;
this->val[1][0]=a10;this->val[1][1]=a11;this->val[1][2]=a12;this->val[1][3]=a13;
this->val[2][0]=a20;this->val[2][1]=a21;this->val[2][2]=a22;this->val[2][3]=a23;
this->val[3][0]=a30;this->val[3][1]=a31;this->val[3][2]=a32;this->val[3][3]=a33;
}
};

template< typename T >
struct M : public B<T{
M() : B<T>(), i() {}
const T* data() const {return
B<T>::begin()->begin();} int i;
};

int main()
{
M<floatmatrix;
matrix.assign(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,1 5);
const float* data = matrix.data();

for( std::size_t i = 0; i < 16; ++i ) {
std::cout << data[i] << '\n';
}

return 0;
}
You could simply verify that the layout is like you expect. In the
constructor of the 'V' class, for example, you could simply do

if (sizeof(this) != sizeof(T) * 4)
throw "A-a-a-a-a-a-a";

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 11 '08 #2
Victor Bazarov wrote:
Hendrik Schober wrote:
[...]
>'M<T>::data()'assumes that all of data members of the inherited
'F's are laid out contiguously. Can we assume this regarding to
the standard?

No. Who knows what kind of padding your compiler sticks in your
'F' class right after the 'val'...
Thanks. I feared so.
What's needed to turn a class into a non-POD type?
[...]
>>
template< typename T >
struct F {
T val[4];
F()
{val[0]=T();val[1]=T();val[2]=T();val[3]=T();} F(T a0, T a1, T
a2, T a3) {val[0]=a0 ;val[1]=a1
;val[2]=a2 ;val[3]=a3 ;} T& operator[](std::size_t i) {return
val[i];} const T& operator[](std::size_t i) const {return val[i];} T*
begin() {return
&val[0];} const T* begin() const {return
&val[0];} T* end () {return
&val[4];} const T* end () const {return
&val[4];} };

Your use of 'flowed' format is apparent here... :-)
???
But let me just say, ugh! Why couldn't you initialise the 'val' at
least in the default constructor? You could do

F() : val() {}

right?
I didn't know you could do this with an array.
(I started with the original code. When I had removed
everything to make it a self-contained example, it already
didn't expose the problem anymore, so I only did a little
cleaning before presenting it here, without going into much
detail. I probably hadn't even looked consciously once at
this ctor.)
[...]
>template< typename T >
struct B : public V< V<T {
typedef V<TRow;

I don't think you're using this typedef. Are you?
It seems it's an oversight of the stripping-down process. :)
[...]
You could simply verify that the layout is like you expect. In the
constructor of the 'V' class, for example, you could simply do

if (sizeof(this) != sizeof(T) * 4)
throw "A-a-a-a-a-a-a";
We already tried. But as soon as any other code is added
to the mix, the problem is going away. It might be GCC's
optimizer choking.
V
Schobi
Nov 11 '08 #3

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

Similar topics

11
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little more today, and found that there is very little...
2
by: Andrea Gavana | last post by:
Hello NG, this may seem a stupid (or even impossible) question, but my knowlegde of Python is quite limited. I have basically a simple graphical user interface that contains a Panel, another...
8
by: TS | last post by:
I am trying to get set a property of a control on the inherited class from base class. I imagine i have to use reflection, so could someone give me the code to do it? something like this?...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
11
by: Henryk | last post by:
I have something like class Params { public: const static char nOne = 1; const static int nTwo = 2; const static char nThree = 3; }; This is just a wrapper for globally used parameters in...
3
by: Jeffrey Barish | last post by:
I have a class derived from string that is used in a pickle. In the new version of my program, I moved the module containing the definition of the class. Now the unpickle fails because it doesn't...
1
by: bruce628 | last post by:
I want to use SWT tab compnent and make it be multiline,but I fail.please see the class TabFolderExample. Can aneone help me? import java.awt.BorderLayout; import...
3
by: Peskov Dmitry | last post by:
Hi, What is the memory layout of an object, and the derived object. For example : class A { private: int prv_data; protected: int pro_data;
36
by: Roedy Green | last post by:
The only browser I have encountered that supports <colgroup><col class="behold"></colgroup> to apply a CSS style to a whole column, is Microsoft Internet Explorer. I have been told it SHOULD NOT...
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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.