Bona Fide OS Development

New & Delete in C++
Page 1 of 1

Author:  ctimko [ Sun Oct 17, 2010 8:31 pm ]
Post subject:  New & Delete in C++

Hi, I just wanted to make this post more as an informative piece. I have begun to notice a LOT of people who attempt to write their Kernel in C++ (which requires extra code for the support of C++) do not completely understand how New and Delete work. They are NOT the same as just calling Malloc and Free. Let me explain.

In C++, the operator "new" does the following:
  • Allocates memory for the storage of the object
  • Calls the Class's constructor to setup the object
Therefore it should call malloc for the space it needs AND then it should call the object's Constructor.

The delete operator doesn't just call free either.
  • Call the Class's DEconstructor to handle space cleanup
  • Deallocate (or free) the memory that was used for storage of the class's instance.

Just calling malloc or free won't do everything that a Class type actually does, and you aren't using C++ in this case, you are just writing in C with the operators.

Now as of now I don't have a working model of this setup, but I am working on one and I will post more when I get it working flawlessly (and securely).

Author:  brenden [ Mon Mar 07, 2011 5:12 am ]
Post subject:  Re: New & Delete in C++

This is an awesome tip, thanks for pointing this out! Someday I might have to toy with C++ OS Dev no matter how bloated it is to support, haha.

Author:  ctimko [ Mon Jun 13, 2011 7:49 pm ]
Post subject:  Re: New & Delete in C++

Yea, actually, I think there is a bit of support code that all of the tutorials I have seen leave out. Building up the C++ libraries from the ground up are difficult. As soon as I get time I am going to look at the libc++ code and copy it...or i am going to statically compile it into my kernel...

Author:  ctimko [ Sun Jul 31, 2011 10:45 am ]
Post subject:  Re: New & Delete in C++

Just wanted to update this. I have spoken with a very intelligent man, and he was kind enough to point out that the calls to the destructor and constructor are inserted into the code by the compiler. He could very well be 100% correct, and I will do some more investigating to determine this, but for the time being you really don't need it so he is 95% correct at the very least.

Page 1 of 1 All times are UTC - 6 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group