Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

Memory Alignment on Objects

Hi all,

I was wondering whether this is valid or not?

class basObject
{
    private:
          char objName[256];
          char className[256];
   public :
          baseObject(char *objName, char *className);
}


class childObject : public baseObject
{
    private :
         char childName[100];
   public :
        childObject(char *childName);
}

may i ask, whether objName and className is aligned before or after the child object?
say
addr : 0x10001000
0x10001000 - Object address of baseObject
0x10001001 - baseObject::objName address
0x10001002 - baseObject::objName[0]
.
.
. and so on...

so, does the child object does the same thing?
say
addr : 0x10001000
0x10001000 - Object address of childObject, which is also baseObject
0x10001001 - baseObject::objName address?
or
0x10001001 - childObject::childName address?

please help me... and pls forgive me if I make any fundamental mistakes...
0
thrawn80
Asked:
thrawn80
1 Solution
 
jacobhooverCommented:
I would have to think that this would be VERY compiler specific.  And even beyond that, flag specific per compiler.

Ex: MS VC++6

#include "stdafx.h"
#include <string>
class baseObject
{
    private:
          char objName[256];
          char className[256];
   public :
             baseObject(char *lobjName, char *lclassName)
             {
                        strncpy(objName,lobjName,256);
                        strncpy(className,lclassName,256);
             }
};
class childObject : public baseObject
{
    private :
         char childName[100];
   public :
             childObject(char *lchildName):baseObject("DERIVEDCHILD","CHILDOBJECT")
             {
                   strncpy(childName,lchildName,100);
             };
};

int main(int argc, char* argv[])
{
      childObject child("PINKY");
      __asm int 3; //HARDSET BREAKPOINT FOR EXAMPLE PURPOSES
      return 0;
}


When evaluating this code,
&child                        = 0x0012fd1c
(LPBYTE(&child))         = 0x0012fd1c == "DERIVEDCHILD"
(LPBYTE(&child)+256)    = 0x0012fe1c == "CHILDOBJECT"
(LPBYTE(&child)+512)    = 0x0012ff1c == "PINKY"

As you can see, the pointer (reference) to the object is in fact the address of the first variable of the base class, which makes sense because the base class is created before the child class.


I tried this logic with several different tests, including:
#pragma pack(2)

class baseObject
{
    private:
          char objName[256];
          char className[256];
                              char ForceAPAGE[1024*8];// NEWP, DIDN'T BREAK IT
   public :
             int DoNothingButModifyTheVectorTable1(){return 1;};
             int DoNothingButModifyTheVectorTable2(){return 2;};
             int DoNothingButModifyTheVectorTable3(){return 3;};
             int DoNothingButModifyTheVectorTable4(){return 4;};// NEITHER DID THESE
             __inline int CanThisBreakIt(){return -1;}
             baseObject(char *lobjName, char *lclassName)
             {
                        strncpy(objName,lobjName,256);
                        strncpy(className,lclassName,256);
             }
};

None of which seemed to effect the location in memory of the objects. (Other than adding the offset for the "ForceAPAGE" variable).

0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now