(C++) Compile time retrival of a class's vtable (solved)

Started by
33 comments, last by kornman00 17 years, 11 months ago
Hi, this is the AP from above. For a while in my serialization lib, I was using placement new with a special empty constructor that took a dummy parameter, but I realized (the hard way) that it doesn't address the issue of array elements inside a class, which have their default constructors called no matter what. So using placement new with a special constructor isn't a bulletproof solution. What I resorted to was having a global bool variable called 'generating_vtables' that triggers an early exit from the default constructor if it's true. It works, and is actually safe since I'm using a code generator to generate the default constructor (among other functions) for all of my serializable classes, but again, is not the most elegant solution. A quick and easy way to grab the vtable pointer without incurring the cost of constructing an object would allow me to generate much cleaner code, but as several people have already said, the language doesn't give us an easy way to it so I shouldn't hold my breath. Thanks anyway.
Advertisement
Quote:Original post by msn12b
Use placement new.

How many times will I repeat myself??

MSN

until you run out of heap space ;p?

I've only delt with boots's parser utilites, so I don't have anything insightful for your problem AP, sorry. Good luck though :)
Quote:Original post by Anonymous Poster
Hi, this is the AP from above. For a while in my serialization lib, I was using placement new with a special empty constructor that took a dummy parameter, but I realized (the hard way) that it doesn't address the issue of array elements inside a class, which have their default constructors called no matter what. So using placement new with a special constructor isn't a bulletproof solution. What I resorted to was having a global bool variable called 'generating_vtables' that triggers an early exit from the default constructor if it's true. It works, and is actually safe since I'm using a code generator to generate the default constructor (among other functions) for all of my serializable classes, but again, is not the most elegant solution. A quick and easy way to grab the vtable pointer without incurring the cost of constructing an object would allow me to generate much cleaner code, but as several people have already said, the language doesn't give us an easy way to it so I shouldn't hold my breath. Thanks anyway.


Well that is certainly an interesting constraint.

Are you trying to serialize any C++ type? If you don't have constraints on the data model you are using, then you'll have to stick with the constraints imposed by the language, which leads to situations like this (not being able to custom initialize array elements).

I.e., either don't allow complicated default constructors, or declare your own array types/templates to allow for this construct.

MSN
Quote:Original post by kornman00
Quote:Original post by LessBread
Compiled code is actually machine code not assembler - but that doesn't change the fact that there is no memory at all at compile time. The closest you'll get is a hardcoded address, based on the assumption that the executable will always load at the same place in the eventual memory address space (on Windows that would be 0x00400000). The address of operator doesn't work by hardcoding the eventual address of a variable. Most typically, it works by using the lea instruction.

For example:

int x = 0;
int *y = &x

; int x = 0;
movl $0,-4(%ebp)
; int *y = &x
leal -4(%ebp),%edi
movl %edi,-8(%ebp)

Compiled code is assembly in byte code form. I'm assuming thats what your trying to say with "machine code", and thats what I meant. The addresses are prefixed, like you said, usually at 0x400000 in EXEs (0x10000000 is the default DLL base address), and thats what I was talking about when I was wanting to the vtable address, since with VC, the vtable is already located at a fixed spot. The compiler (VC anyways, which is what I base everything off of) sticks the vtable of whatever class at a fixed address in the .data (or .rdata, I forget which now) segment of an EXE.
And in static data code (or the use of static data), the address-of operator does work by using the address of whatever data. Unless its with a C++ compiler, which sometimes has to make use of code that is ran before the actual call to the main method, but most of the time the compiler can create data that is already setup and doesn't need and preprocessing.


Yes, byte code. Doesn't the linker actually build the eventual pe file sections?
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Quote:Original post by LessBread
Yes, byte code. Doesn't the linker actually build the eventual pe file sections?


AFAIK, it builds the actual PE file and assoc. data. Its the last thing VC runs when I go to compile anyway ;x

This topic is closed to new replies.

Advertisement