Advertisement

memory leaks .. ?

Started by June 04, 2002 05:58 PM
13 comments, last by Xces 22 years, 8 months ago
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??
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.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Advertisement
Ok i get that point, So when i use this:

char *temp[255];

I do not need to "clear" it?
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.
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
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
Advertisement
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
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]

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

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...
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.)
---visit #directxdev on afternet <- not just for directx, despite the name

This topic is closed to new replies.

Advertisement