Bona Fide OS Developer
View unanswered posts | View active topics It is currently Tue Mar 19, 2024 3:38 am



Post new topic Reply to topic  [ 4 posts ] 
 New & Delete in C++ 
Author Message

Joined: Wed Oct 14, 2009 9:39 am
Posts: 198
Location: United States
Post 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).

_________________
Charles Timko
push %esp ;Musings of a computer addict


Sun Oct 17, 2010 8:31 pm
Profile WWW
Site Admin

Joined: Fri Jul 24, 2009 10:02 pm
Posts: 247
Location: Las Vegas, NV, US
Post 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.


Mon Mar 07, 2011 5:12 am
Profile

Joined: Wed Oct 14, 2009 9:39 am
Posts: 198
Location: United States
Post 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...

_________________
Charles Timko
push %esp ;Musings of a computer addict


Mon Jun 13, 2011 7:49 pm
Profile WWW

Joined: Wed Oct 14, 2009 9:39 am
Posts: 198
Location: United States
Post 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.

_________________
Charles Timko
push %esp ;Musings of a computer addict


Sun Jul 31, 2011 10:45 am
Profile WWW
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin and tweaked by the BF Team.