Memoryleak

Started by
13 comments, last by JohanK 22 years ago
It''s not wasted...if you need it, you need it. You just need to be smart about how you use it. There are certain things you can''t avoid allocating memory for...you know? :-)

-------------------
"Pointer?????"
-Anonymous

-=Xelius=-
-------------------"Pointer?" -Anonymous-=Xelius=-
Advertisement
I know I need to allocate memory, the question is wheter (or however you spell that) or not it was deallocated by some magic ferry.

It would suck if my app allocated a MB each minute and not deallocating it. (that could easily be the case)

But as I learned here, when you new it you have to delete it. Thats fine as long as I know it...

Gee, about that example you gave, I think it would have been clearer if you did:

(Main)
cout << "myA = " << myA << "\n";
(Function)
cout << "ret = " << ret << "\n";
(Main)
cout << "myA = " << myA << "\n";

This would demonstrate that Function() returns a pointer
to the newly allocated memory. The addresses of the pointers
will never change, but what they point to will. Was this
your intention?

I also had a question to just clear things up in my mind. If memory is allocated (via New) in a function (like Function()), the pointer to the new memory can be returned. However, when is that memory gauranteed to be safe and secure? I am trying to remember the cases when the memory myA newly points to might get overridden, and when it will not. Does this question make sense?
Yeah, I think I understand what you mean. Basically, any memory you allocate within your program via new is (in most cases) safe to pass around within your program. However, if you have code like this:

GameObject *DoStuff(ControlStruct *s){    GameObject g;    // .... perform actions on g    return &g} 


This is not valid because when the function exits, any local variable is considered non-existant. And it may be possible that you can access the contents of the returned address properly for a little while because it''s location in memory has not been over-written. However, this behaviour is NOT guaranteed and therefore shouldn''t be used! :-)

Does this clear things up? Or would you like some further explaination?
-------------------"Pointer?" -Anonymous-=Xelius=-
Yup, I think I got a good grasp on this now.
Thanks for all the help! This forum is the best I''ve ever used!

This topic is closed to new replies.

Advertisement