Dynamically allocating C++ objects.

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
viral

Dynamically allocating C++ objects.

Post 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
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:Dynamically allocating C++ objects.

Post by Colonel Kernel »

You have to overload operator new so that it calls your custom malloc.
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
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Dynamically allocating C++ objects.

Post 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 ) ).
Every good solution is obvious once you've found it.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:Dynamically allocating C++ objects.

Post 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?
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!
rootel77

Re:Dynamically allocating C++ objects.

Post 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

Code: Select all

myclass = new MyClass();
and the allocation failed (ie your malloc has returned null), the compiler will not call the constructor and wil return null;
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Dynamically allocating C++ objects.

Post 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. ;-)
Every good solution is obvious once you've found it.
Post Reply