Class members padding

Get answers to all your basic programming questions. No Ogre questions, please!
Post Reply
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Class members padding

Post by Klaim »

In Ogre::Vector3 we can see that :

Code: Select all

class _OgreExport Vector3
    {
    public:
		Real x, y, z;
But then :

Code: Select all

	inline Real operator [] ( const size_t i ) const
        {
            assert( i < 3 );

            return *(&x+i);
        }

		inline Real& operator [] ( const size_t i )
        {
            assert( i < 3 );

            return *(&x+i);
        }

I'm almost sure that C++ suggest that we don't assume the members of a class to be contigu because of the compiler can optimize the memory structure by adding some bytes between them. Right?

So those functions are not safe to use on all architectures, aren't they?
Olemars
Greenskin
Posts: 145
Joined: Wed Jun 10, 2009 8:09 am

Re: Class members padding

Post by Olemars »

Interesting catch. Might be better suited for developer talk really.

From what I gather, assuming the memory layout to be contiguous would be valid if the class could be considered an aggregate POD class. However, Vector3 doesn't fulfill the standard definition, since it's got both user defined operator= and constructors. Apparently it works OK anyway on x86 systems, but there are no guarantees. Since Ogre is supposed to also run on mobile platforms whose compilers might do all kinds of tricks, I'd say this is an unsafe assumption to make.
User avatar
Ruud v A
Gremlin
Posts: 150
Joined: Mon Jan 28, 2008 6:44 pm
Location: The Netherlands
x 5
Contact:

Re: Class members padding

Post by Ruud v A »

I doubt optimisation would add extra bytes, but it is however bad practice to do things like this (for reasons mentioned above).
User avatar
madmarx
OGRE Expert User
OGRE Expert User
Posts: 1671
Joined: Mon Jan 21, 2008 10:26 pm
x 50

Re: Class members padding

Post by madmarx »

Yes good catch.
the compiler can optimize the memory structure by adding some bytes between them
It can also add bytes in order to store classes/object informations (no joke) in some extrems cases (I read that on soustrup FAQ). So it depends on the compiler, more than the targeted architecture.
So yes, it is a real error.
Tutorials + Ogre searchable API + more for Ogre1.7 : http://sourceforge.net/projects/so3dtools/
Corresponding thread : http://www.ogre3d.org/forums/viewtopic. ... 93&start=0
User avatar
Chris Jones
Lich
Posts: 1742
Joined: Tue Apr 05, 2005 1:11 pm
Location: Gosport, South England
x 1

Re: Class members padding

Post by Chris Jones »

Maybe i'm wrong but padding is usually only done to make the data align to certain boundaries (e.g. a 4 byte value would be aligned to a multiple of 4 bytes). In this case Real will be 4 or 8 bytes so shouldn't need padding. Any additional data/padding for the class would be before or after those members which shouldn't affect alignment.
User avatar
syedhs
Silver Sponsor
Silver Sponsor
Posts: 2703
Joined: Mon Aug 29, 2005 3:24 pm
Location: Kuala Lumpur, Malaysia
x 51

Re: Class members padding

Post by syedhs »

Chris Jones wrote:Maybe i'm wrong but padding is usually only done to make the data align to certain boundaries (e.g. a 4 byte value would be aligned to a multiple of 4 bytes). In this case Real will be 4 or 8 bytes so shouldn't need padding. Any additional data/padding for the class would be before or after those members which shouldn't affect alignment.
That's my thought exactly - I have noticed about this for quite sometime.
A willow deeply scarred, somebody's broken heart
And a washed-out dream
They follow the pattern of the wind, ya' see
Cause they got no place to be
That's why I'm starting with me
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: Class members padding

Post by Klaim »

Yes but I was (as others) thinking about non-pc-mac platforms...

I guess someone would have pointed the problem if it was blocking a new platform portage.
Post Reply