C++ Variable and an Error

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Tolga

C++ Variable and an Error

Post by Tolga »

Hi. I'm writing my kernel with C++. (GCC) I wrote a simple video class for text mode. When i am write my code same this:

//------------- C++ ---------------
tvideo video;

int main()
{
video.clear();
}

//----------------------------------

My kernel is halting.

But when i am write it same this, it is working.

//------------ C++ ----------------
int main()
{
tvideo video;
video.clear();
}
//---------------------------------

What is the matter? Why it is halting? I tried in bochs, on real pc and wmvare.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:C++ Variable and an Error

Post by Colonel Kernel »

What have you done to ensure that static constructors are being called before main() is run? As the implementor of the OS, such C++ run-time support is your responsibility to implement.

This might help.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven
Contact:

Re:C++ Variable and an Error

Post by Candy »

Tolga wrote:Hi. I'm writing my kernel with C++. (GCC) I wrote a simple video class for text mode. When i am write my code same this:

//------------- C++ ---------------
tvideo video;

int main()
{
video.clear();
}

//----------------------------------

My kernel is halting.

But when i am write it same this, it is working.

//------------ C++ ----------------
int main()
{
tvideo video;
video.clear();
}
//---------------------------------

What is the matter? Why it is halting? I tried in bochs, on real pc and wmvare.


As quoted above, you need to call the constructor before you call anything (specifically: that dereferences the this-pointer at some time) on the given object. When the object is on the stack, the constructor call is added to the start of the function and the object is on the stack (destructor at end, etc). When it's a global object, its constructor can't be added to "the global function" since it doesn't exist. Instead, the global function is emulated by the startup code (crt0.o) by calling all functions in .ctors and upon exit, calling all functions in .dtors. These are sections in your executable (or something similar). Your code doesn't call these constructors, whereafter the object itself contains garbage or zeroes (depending on whether you zero the memory or not) and any information in the garbage/zeroes might cause any type of behaviour, including crashes. In some cases it might go completely unnoticed, although the C++ standard doesn't require this. I know that GCC does not explicitly check the "this"-pointer.

Long story short, call all constructors in .ctors and see it work just the same.
Post Reply