Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

OpenGL_Guru

C++ Tip #1: Allocating Memory C++ new and delete

This topic is 5394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i thought i would like to share a tip for all the C++ programmers out there.. ill post this one for now.. ive done some reading and some test code..and thought id share..this one is very interesting and easily overlooked. What's wrong with this picture? ..... //include files yadda yadda string *myStringArray = new string[100]; ... delete stringArray; //do next thing, whatever that is. Everything here appears to be fine and the code looks nice and clean, the use of new matches the use of delete, but something is terribly terribly wrong... ill just wait a little bit to see if anyone who knows would like to jump in and maybe take a stab at the problem.. [edited by - opengl_guru on January 13, 2004 1:23:41 PM]

Share this post


Link to post
Share on other sites
Advertisement
You''re deleting the wrong variable, and you''re deleting it the wrong way.

You should delete[] myStringArray;

If you overloaded all your "new"s like a good boy, and threw in an assert when you used the wrong one, you''d never have to worry about getting it wrong again.

Share this post


Link to post
Share on other sites
hehe good job.. one of my co-workers got slammed when he ran his program and i had to break the news to him...

does anyone still use malloc? and if you do how do you find it useful?

Share this post


Link to post
Share on other sites
malloc() is functionally equivalent to new char[] (except, of course, for exception semantics and freeing procedures). There''s no reason to use it, and if you''re using new or delete at all, it can only make things more confusing and buggy.

Share this post


Link to post
Share on other sites
Apparently, your company needs to invest in copies of Effective C++.

quote:
Original post by OpenGL_Guru
does anyone still use malloc? and if you do how do you find it useful?
I use malloc when I program in C (rarely). How do I find it useful? How do you find new useful? That''s a "journalist" (ie, dumb) question.

Share this post


Link to post
Share on other sites
typedef string myStrings[4];

string *myString = new myStrings;

//so in this case which delete statement is correct..same example but using typedefs and why ?

delete myString;
delete [] myString;


[edited by - opengl_guru on January 13, 2004 1:34:01 PM]

Share this post


Link to post
Share on other sites
Well... if you''re programming drivers or low level things like I am at work right now then you''re not going to be using new and delete

But for most normal application programming new and delete (and new [] and delete []) are the way to go. Actually, for applications I''d say C# on windows systems and java on unix/linux systems is the way to go nowadays. Forget the whole delete operator all together I say!

-out

Share this post


Link to post
Share on other sites
thats one of the books that i have moderator, among other things... its not that i didnt know the answer i just thought i would share the knowledge with maybe those that didnt have such books or were new to C++. thats all hope thats ok

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!