• Advertisement

Archived

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

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

This topic is 5151 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
Try:

int x = 10;
int y = 10;
char **ppArray = (char **)new char[x * y];

ppArray[2][3] = '3';

In an MFC class this will crash, but:

int x = 10;
int y = 10;
char **ppArray = (char **)calloc(sizeof(char), x * y);

ppArray[2][3] = '3';

Works just fine and it zeros out the memory without having to overload the new operator.

That's why people use malloc!


[edited by - citizen3019 on January 13, 2004 1:38:34 PM]

Share this post


Link to post
Share on other sites
I''m assuming you meant string *myString = new string;
I''m not actually sure which is correct here. I''m going to guess the array version of delete is right, which is a nasty gotcha and one reason to be careful with typedefs. But since you''re supposedly giving us a wonderful tip maybe all you need is plain old delete. I dunno

-out

Share this post


Link to post
Share on other sites
except malloc and free dont know about contructors or destructors -- which one person pointed out.. as you are asking for headaches mixing new delete, malloc and free...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
wow!! thanks for educating us with this amazing C++ tip....

I think this post would be better on the beginners forum.

And sack the person who you work with.

Share this post


Link to post
Share on other sites
people sure seem to be in a jumpy mood today.. geez i was just trying to be a little helpful, didnt know i was gonna get flamed for it.

Share this post


Link to post
Share on other sites
no BillPo the code is right, in my string declaration..

delete [] myString; -- this would be the correct one of the two.

[edited by - opengl_guru on January 13, 2004 1:43:14 PM]

Share this post


Link to post
Share on other sites
It''s good to be helpful, but especially with beginners material like this, it''s often better to wait to be asked. I daresay there are few C++ programmers on this board who don''t know the difference between delete and delete[].


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
oh, haha, right. my bad. I haven't used C/C++ in so long that I forgot the syntax of typedefs... weeeeeeee
Anyway, yeah, so now it makes more sense. And yes, delete [] myString;. But isn't that more confusing, because now you have code that looks like
strings *ar = new MyStrings;   // not obvious that this is an array
...
delete ar; // its not obvious that we should be using [] here


Sneftel: You again! /me squints and stares down sneftel

-out

[edited by - billpo on January 13, 2004 1:56:54 PM]

Share this post


Link to post
Share on other sites
yep thats right.. thats why i try to shy away from typedefs when dealing with arrays if i can help it.. well i hope SOMEONE out there got something out of this thread..

Share this post


Link to post
Share on other sites
quote:
Original post by BillPo
Sneftel: You again! /me squints and stares down sneftel


/me whistles innocently, suddenly hurls a smoke grenade to the floor, and escapes in the confusion


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites

  • Advertisement