Page 1 of 1
Dynamically allocating C++ objects.
Posted: Thu May 04, 2006 5:54 pm
by viral
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...
Code: Select all
//this works
MyHelloClass h1;
h1.callThisFunction();
//this doent work
MyHelloClass *ptr;
ptr = (MyHelloClass *) kernel_malloc(sizeof(MyHelloClass));
ptr->callThisFunction(); //crashes
Re:Dynamically allocating C++ objects.
Posted: Thu May 04, 2006 6:49 pm
by Colonel Kernel
You have to overload operator new so that it calls your custom malloc.
Re:Dynamically allocating C++ objects.
Posted: Fri May 05, 2006 12:07 am
by Solar
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 ) ).
Re:Dynamically allocating C++ objects.
Posted: Fri May 05, 2006 12:22 am
by Colonel Kernel
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?
Re:Dynamically allocating C++ objects.
Posted: Fri May 05, 2006 3:18 am
by rootel77
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
and the allocation failed (ie your malloc has returned null), the compiler will not call the constructor and wil return null;
Re:Dynamically allocating C++ objects.
Posted: Mon May 08, 2006 2:01 am
by Solar
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.