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.
Hi..
I have a prob with my C++ desing. All my C++ objects which are declared globally work fine.. but when i try dynamic allocation it crashes with:
exception(): 3rd (14) exception with no resolution, shutdown status is 00h, resetting
I guess the problem is that when object is defined globally or statically, the constructor is being called. But during dynamic allocation the constructor may not be called...
The problem isn't that operator new() might be unavailable - in that case the first code fragment wouldn't work either. The problem is that the constructor never gets called!
What you are doing here, you are allocating a memory area big enough to hold an instance of MyHelloClass - and then jump into that uninitialized memory area. You just have a bunch of bytes, you don't have a MyHelloClass object yet.
The solution, of course, is using new( MyHelloClass ) instead of kernel_malloc( sizeof( MyHelloClass ) ).
Every good solution is obvious once you've found it.
Solar wrote:The problem isn't that operator new() might be unavailable - in that case the first code fragment wouldn't work either.
Which is the first code fragment? The first part declares an object on the stack... What does this have to do with new?
The problem is that the constructor never gets called!
Exactly, because that's the "new operator's" job. Not to be confused with "operator new", which is what the OP can overload to call his kernel_malloc(). I seem to remember Scott Meyers mentioning that subtle difference in one of his books...
The solution, of course, is using new( MyHelloClass ) instead of kernel_malloc( sizeof( MyHelloClass ) ).
You mean new MyHelloClass() right?
Top three reasons why my OS project died:
Too much overtime at work
Got married
My brain got stuck in an infinite loop while trying to design the memory manager
dont forget to compile with -fcheck-new to force the compiler to check the return value of 'new' before calling the constructor. The default behaviour here, as stated by the ISO c++ standard, is that the operator new should throw a bad_alloc excpetion (unless it is declared with throw() ), so the compiler doesn't insert the check for the new's return value.
unless you have support for the exception code you have to set -fcheck-new so if you write the following statement
Colonel Kernel wrote:
Which is the first code fragment? The first part declares an object on the stack... What does this have to do with new?
I admit I was under the impression that local object creation also happens under control of new(), and that I haven't delved into the C++ runtime deep enough to know for sure.
The solution, of course, is using new( MyHelloClass ) instead of kernel_malloc( sizeof( MyHelloClass ) ).
You mean new MyHelloClass() right?
Erm... yes. See, I'm working with C++ code all day, but I almost never use new() either way (i.e., good code). Call me a bit rusty around the edges.
Every good solution is obvious once you've found it.