Page 1 of 2
C++ Class Objects in an OS
Posted: Mon Nov 18, 2002 6:03 pm
by PlayOS
Hi,
I was just wondering if it is possible to create an instance of an object anywhere in memory that you want, like specify the exact address that you want it to be put. I need to do this in my OS so what kind of C++ support qould be required, if it is possible.
Also, does anyone know where I can get some docs about how classes are created in memory, like something that explains exactly what a class looks like in memory, how it is accessed and things like that.
thanks.
Re:C++ Class Objects in an OS
Posted: Tue Nov 19, 2002 7:09 am
by Tim
PlayOS wrote:I was just wondering if it is possible to create an instance of an object anywhere in memory that you want, like specify the exact address that you want it to be put. I need to do this in my OS so what kind of C++ support qould be required, if it is possible.
Sure, for objects created on the heap, at least:
Code: Select all
operator new(size_t size)
{
return any_address_you_want;
}
Also, does anyone know where I can get some docs about how classes are created in memory, like something that explains exactly what a class looks like in memory, how it is accessed and things like that.
Check your compiler documentation. There's no set standard. Usually an instance of a class looks like the equivalent structure. Adding virtual methods puts a virtual method table somewhere (before or after the class data). However, you shouldn't usually need to know objects' binary layout unless you're trying to interface to C++ from C or assembly.
Re:C++ Class Objects in an OS
Posted: Tue Nov 19, 2002 8:25 pm
by PlayOS
Thanks, the reason I want to know the layout of a class in memory is because I want to be able to copy the class in memory to a different location, then use that object as a seperate instance, I am just experimenting with some ideas and wanted to see if this would work, can you tell me if a object pointer points to the start of the object in memory.
Like say I create an instance of a class:
class clsInstance;
Would &clsInstance return the address of the class in memory so that I could do something like this to copy it:
char* clsptr = (char*)0x100000;
memcpy(clsptr, (char*)&clsInstance, sizeof(class));
then cast clsptr to type class, then use it as a seperate instance.
I have tried this and it partly worked, the reason I know this is because the class method that I called actually partly executed, the result was displayed on the screen.
anyway, thanks for your help. I hope that I am clear enough that you can understand what I am trying to do.
Re:C++ Class Objects in an OS
Posted: Tue Nov 19, 2002 8:39 pm
by PlayOS
OK, I went back to my source and realised that I forgot to cast it, now that I have casted it everything goes according to plan.
I wasn't sure if it would work but it does.
thanks again.

Re:C++ Class Objects in an OS
Posted: Wed Nov 20, 2002 7:23 am
by Tim
Sure, you could do that. But you'd be better off using the C++ assignment operator, which does more or less the same thing.
Re:C++ Class Objects in an OS
Posted: Wed Nov 20, 2002 10:28 am
by Tom
I've never had to use the "operator" operator (

hmm that sounds funny ).
The default operators work enough for me, at least, but I did read that the are a way to control your code more.
I really don't see a need for the "operator" operator, unless your writing your own lib or making a C++ kernel with the "new" operator.
Re:C++ Class Objects in an OS
Posted: Wed Nov 20, 2002 12:13 pm
by Tim
That's exactly the point. If you're writing a kernel in C++, you need to overload new and delete, otherwise they won't work.
Re:C++ Class Objects in an OS
Posted: Thu Nov 21, 2002 7:48 am
by PlayOS
Thanks Guys,
I just have a couple more questions:
1) Does anyone know why there are 2 Constructors and Destructors created for a class?
2) How would I go about writing code for member functions in asm?
I would rather not use inline asm if possible.
I have been searching for some answers but I have not been able to find anything, maybe I am not using good search strings.
thanks.
Re:C++ Class Objects in an OS
Posted: Thu Nov 21, 2002 8:11 am
by Tom
Well..I've only made classes with 1 constructor and 1 destructor...so I don't know what your talking about...here's how to call class functions in NASM:
It's just too tricky...instead, call a "extern "C" " function and your classe's function calles that function. But if you really want, i'll look it up...
Re:C++ Class Objects in an OS
Posted: Thu Nov 21, 2002 8:25 am
by PlayOS
If you disassemble your code files or look at the exported names in your object file, you will see that there have been 2 constructors and destructors, even if you only have one defined.
I am not sure why, but this is what is in my code after compilation.
thanks.
Re:C++ Class Objects in an OS
Posted: Thu Nov 21, 2002 10:00 am
by whyme_t
ok, i
think this what you need to do in your inlined asm function (this is the way I've done it

) :-
Code: Select all
someASMmemberFunc
push ebp
mov ebp, esp
push esi
push ebx
;; do what you want here
pop ebx
pop esi
pop ebp
ret
In your C++ code :-
Code: Select all
//asm code inlined into the C++ function
extern "C" inline void someASMmemberFunc(void);
void SomeClass::someMemberFunc(void)
{
someASMmemberFunc() ;
}
Note: I have totally ignored leading _'s in this code
Re:C++ Class Objects in an OS
Posted: Thu Nov 21, 2002 10:19 am
by whyme_t
and another thing. You won't, or at least I don't think, that you can access the data members of your class in your ASM. [ebp+8] will only give the address of the parameter passed in (is this right??). So you would need to do anything with you data members before and after your asm call, and pass in and out any data that you need to use in your asm code.
Someone want to confirm this?

Re:C++ Class Objects in an OS
Posted: Thu Nov 21, 2002 11:25 am
by Tim
You have several options for mixing assembly and C++:
1) Don't bother; write everything in C++
2) Write any assembly using the C++ compiler's inline assembler
3) Reverse engineer your compiler to work out the function and variable name decoration scheme and the layout of classes and vtables in memory, and do everything in assembly.
I recommend (1) with a little bit of (2). You are effectively trying to do (3).
Re:C++ Class Objects in an OS
Posted: Thu Nov 21, 2002 1:46 pm
by adeelmahmood1
If you disassemble your code files or look at the exported names in your object file
well just a question ;D how do u guys disassemble the code or may be the question is what do u mean by disassembling a code?

Re:C++ Class Objects in an OS
Posted: Thu Nov 21, 2002 2:16 pm
by Tom
ndisasm somefile.bin # disasm somefile.bin - see the binary code in asm.