Problem with class member layout (footprint)

This is a discussion on Problem with class member layout (footprint) within the C++ Programming forums, part of the General Programming Boards category; So, I'm working on this scripting engine... Now, here are a few classes I have; they're all defined in an ...

  1. #1
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69

    Problem with class member layout (footprint)

    So, I'm working on this scripting engine...
    Now, here are a few classes I have; they're all defined in an overlaying Script class:
    Code:
    class Parser{
    protected:
    
    	// methods...
    };
    
    
    
    class CppClass: public Parser{
    private:
    
    	class Operator{
    	private:
    
    		unsigned int udw_return_flags;
    		unsigned int udw_arg_flags;
    		Script::Operator *pO_op;
    
    	public:
    
    		Operator(void);
    	};
    
    	unsigned int udw_flags;
    	String S_name;
    	List<Script::CppBaseFunction> LtCBF_methods;
    	List<Script::CppBaseFunction> LtCBF_statics;
    	List<Script::CppClass::Operator> LtO_operators;
    	List<Script::Variable> LtV_variables;
    
    	// methods...
    
    protected:
    
    	Script::Variable V_return;
    	List<Script::Variable *> *pLtpV_args;
    	List<Script::Variable> LtV_statics;
    	Script *pS_engine;
    
    	// methods...
    
    public:
    
    	// methods...
    
    	friend class Script;
    };
    
    
    
    class Block{
    protected:
    
    	unsigned int udw_flags;
    	Script::Clause *pC_parent;
    
    	// methods...
    
    public:
    
    	List<Script::Variable *> LtpV_variables;
    
    	// methods...
    
    	friend class Script;
    };
    
    
    
    class Variable: public Script::Block{
    private:
    
    	Script::Block *pB_pointer;
    	Script::Clause *pC_parent_real;
    	Script::Class *pC_class;
    	String S_name;
    	double d_index;
    
    	// methods...
    
    public:
    
    	double d_number;
    	String S_string;
    
    	// methods...
    
    	friend class CppClass;
    	friend class Script;
    };
    The problem is, when I do the following, I get two different numbers output - which sucks, since I'm using the udw_flag member to determine whether it's a class, variable, etc...
    Code:
    Script::Variable V_test;
    V_test.udw_flags = 777; // some random number
    
    Script::CppClass *pCC_test = (Script::Variable *)&V_test;
    
    printf("\nflags: %u", V_test.udw_flags);
    printf("\nflags: %u", pCC_test->udw_flags);
    So, I take it the compiler rearranges the members and/or in some other way changes the footprint of the classes...
    Can I prevent this with some command? (I'm using VC++ 7.0 by the way.)

    Help, pretty please?
    Last edited by Osaou; 07-23-2006 at 10:40 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    What are you trying to do? Why are you casting there? CppClass and Variable are two different classes, so if you have a Variable, use its udw_flags, and if you have a CppClass use its udw_flags.

    Or have them both derive from the same base class and put the flags in that base class.

    I don't understand why you'd want to convert one type to the other.
    Last edited by Daved; 07-24-2006 at 12:10 AM.

  3. #3
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    I'm not trying to convert from one class to the other. I just have a list of lots of Script::Variable pointers that can infact be pointing to a Script::CppClass instance... I know I could make them both inherit from a base class with udw_flags defined in it - and I have implemented that as of yesterday - but still, I'd like to know if there's anything I can do to prevent this stuff...

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    There is no reason you should ever have a Script::Variable pointer pointing to a Script::CppClass instance. They are two completely different, unrelated classes. I don't even think that is legal, you are forcing the compiler to accept it with the C style cast, but it will yield undefined behavior. It's like having an int pointer pointing to a string.

    Again, what are you trying to accomplish by storing CppClass addresses in a Variable pointer? Are you trying to make a list of Variables that could be different things, including possibly a CppClass? If so, then Variable would be a base class (either directly or indirectly) of CppClass, or they would share a common base class. There are other ways to accomplish that, but fixing your inheritance hierarchy is probably the best bet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 09:17 AM
  2. class layout guarantees
    By drrngrvy in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 04:41 AM
  3. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21