Jump to content
  • Advertisement

Archived

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

Xces

memory leaks .. ?

This topic is 5888 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

Hey there, i originally programmed in VB, and have no idea how to _un-initialize_ values in C++. I heard that when i don''t do this, for many times, that my memory gets "eaten" which sounds logical to me... Any tips/urls/howto''s??

Share this post


Link to post
Share on other sites
Advertisement
The main point to remember when using C++ is that if you allocate something with malloc(), you must free it with free(), and if you allocate something with new, you must free it with delete.

Share this post


Link to post
Share on other sites
Ok i get that point, So when i use this:

char *temp[255];

I do not need to "clear" it?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Err... I don''t think that syntax will work exactly, it will create an array of pointers, not a string which is 255 characters long.

I don''t know if you understand it already, but a variable defined with a "*", ie. "char *item;", is not actually a variable, but a pointer to a variable. Whereas "char item" is actually a variable. Go buy a book, that''s the best way to learn.

The proper way to do things is as such-


char *var;
var = new char[255];

/* do stuff with variable here */

delete var;
var = new char[8192];

/* do more stuff, with newly allocated space */

delete var;


Or, you can do it more traditionally, not using pointers-


char var[255];

/* do stuff with variable */


And the above code will automatically clear your allocated space for you. It''s only that pointers are more dynamic (ie, you can create and delete them at will) and a lot faster that people use them. Also, the pointers themselves can be "copied"... ie, you can simply say;


char *var1;
char *var2;

var1 = new char[255];

/* set variable */

var2 = var1;

/* var1 and var2 now point to the same space, you can muck around with them as if they were essentially the same */

delete var2;


Hope that helps.

Share this post


Link to post
Share on other sites
Just a small remark to what AP said.
When you would like to delete an array that was dynamically allocated with ''new'', you should use delete[].
So th code could be:

  
char *var;
var = new char[255];
/* do something with var */
delete[] var;

But just as he said: it''s best to buy a good book and read carefully through it.
Success!

===========================
UNKNOWN caused an invalid page fault in module unknown at 0000:bff80eb6
: win98 with multiple sclerose

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
When you delete a pointer, it''s usually smart to set the
value to NULL, so you know it''s invalid.

Assuming you do that, note that in the previous example
where the pointer is copied, the memory pointed to is not!

Since both pointers point to the same block, doing
delete on one of them will also invalidate the other

/Kitt3n

Share this post


Link to post
Share on other sites
char *p_char

p_char = new char(''a'');

don''t deinitialize with delete []

but with delete p_char;

char *p_char_array

p_char_array = new char[50];

delete [] p_char_array

hope this is oke

Share this post


Link to post
Share on other sites
Speaking of memory leaks here.... What would me a good way of detecting them? Is there a handy little func in VC++? I hope it''s not in the debugger. For some reason the debugger and my program don''t work together.

Sander Maréchal
[Lone Wolves Production][Articles][E-mail]

Share this post


Link to post
Share on other sites
To be honest that was the reason why i started this thread. I am kind new to C++, in VB all is done for you...

Share this post


Link to post
Share on other sites
quote:
Original post by smarechal
Speaking of memory leaks here.... What would me a good way of detecting them? Is there a handy little func in VC++?


Sure there is. Put this before your code:

  
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>

And turn it on:

_CrtSetDbgFlag(_CrtSetDbgFlag(0) | _CRTDBG_LEAK_CHECK_DF);

If you have the leaks, you''ll see them in the debug output.
quote:

I hope it''s not in the debugger.


It''s not exactly in the debugger, but it writes to the debug output. It''s a debugging feature, after all.
quote:

For some reason the debugger and my program don''t work together.


You better make them work together, debugging with a debugger is much more efficient than doing so with printf statements (or log files, etc. etc.)

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!