Jump to content
  • Advertisement
Sign in to follow this  
ClementLuminy

Advanced C++ problem ( code stripping )

This topic is 4239 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I'm currently writting my own Real Time Type Information (RTTI) system ( in order to have a FULL control over what is done or not inside my system ). To achieve this, each class store an internal static data member which represent the "type" of the object: class CClass { public: ... private: ... static CTypeInfo m_TypeInfo }; In order to make my rtti system work correctly, every CTypeInfo constructor MUST be called, even if the object which own the CTypeInfo is not referenced in the program. The problem come from the fact that the compiler (visual c++ 2005) strip all the code which is not referenced, so, if if i define a CTypeInfo for a class which is not "referenced", the CTypeInfo constructor will never be called. I have try to use the /OPT:REF linker option, but it do not change anything. I know that this problem sound weird, but i really need to solve it. If you ever encounter this problem, or if you have any hints, please let me know. Sincerly Clement Vidal

Share this post


Link to post
Share on other sites
Advertisement
If the constructor has no side-effects and the constructed object is never used, then the removal of the constructor call should not have an effect on your program, so you need not worry about it.

If the constructor has a side-effect, or the constructed object is used by the program, then the optimizer is not allowed to remove it. If it does, it's an optimizer bug, about which you should notify the compiler writer.

Edit (Small clarification): even if your constructor is kept and called, the standard makes no provision about the time when it will be called. In particular, the order of calls between different constructors is not defined. Also, the program entry point may be reached before all constructors are initialized.

So, why does the constructor need to be called?

Share this post


Link to post
Share on other sites
Thanks for your answers.

In my system the CTypeInfo constructor fill all the informations of the CTypeInfo data:

class CTypeInfo
{
public:

CTypeInfo( CString& typename, CTypeInfo** basetypearray, UInt8 basetypecount);

};


Normally, at the end of the creation of every CTypeInfo , i should have i kind of tree representing all the informations about all the type class in my system(including inheritance)
( in my case i do not care about the order in which CTypeInfo constructor are called )

The problem, is that, because some constructor call are stripped, some Type Information are not included in this graph.

I'm not sure, but i don't think that it come from a compiler bug, maybe the compiler is "too smart" and he strip code that shouldn't be stripped ...


Thanks again

Clement

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
this is how i made my own rtti:

i wrote a lib that stores the rtti-info in a tree.
and, it only stores 2 things: the actual type of an element, and the parent(s).

class MyClass : public BaseClass
{
MyClass()
{
if(NULL == RTTI_GETPARENT("MyClass"))
{
RTTI_ADDCHILD("MyClass", "BaseClass");
}
RTTI_ADD("MyClass", this);
}

};

i can also do:
bool RTTI_ISCHILDOF("MyClass", "BaseClass");
(this works with the whole tree, not only with 2 classes)

it has 2 advantages over the built-in mechanism:
1) only the classes are stored that are actually needed later
2) i only store the infos i really need for a class

and thus, its fast enough for me. and i can optimize the lib later (when needed).
it works for me... but maybe you want to do more complicated things.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!