Jump to content
  • Advertisement

Archived

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

ekrax

just a quick question on dynamic allocation

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

alright i have a function that returns a pointer and withen this function i dynamically allocate an integer array, and i want this function to return this pointer. however obviously i cannot delete it before i return it, or return it before i delete it. is this memory automatically deallocated for me, or must i do this another way to avoid a memory leak. thanks for any help in advance.

Share this post


Link to post
Share on other sites
Advertisement
I assume you're using C++? If so, when you allocate memory with the keyword "new" it gets created on the freestore. This memory is not scoped, so once it's created it will stay there until you delete it or until your computer shuts off. If you're using Java, it will stay there until the garbage collector disposes of it, which only happens if the VM detects that no pointers are referencing the allocated memory.

If you allocate memory dynamically (in your case, an integer array) in a function and have that function return a pointer to it, the pointer references the memory created in the freestore (which does not have any scope). But if you don't save the pointer that the function returns, then you have no way of accessing that array in the freestore. That's what a memory leak is.

When you're done with the array and ready to deallocate it, just call "delete [] myarray" where "myarray" is the pointer your function returned.

I highly suggest reading over your C++ book with a fine tooth comb. If it doesn't have written in it any of the above information, you should consider getting another one. I reccomend "C++: The Complete Reference, 4th Edition" by Herbert Schildt. It is *THE* comprehensive book to programming with C++.

EDIT: Added hyperlink
_________________
:: MajorShredd ::
The glass is neither half full nor half empty;
rather, it is a combination of both, and the system is perfect.


[edited by - MajorShredd on June 1, 2004 10:50:47 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by MajorShredd
I assume you're using C++? If so, when you allocate memory with the keyword "new" it gets created on the freestore. This memory is not scoped, so once it's created it will stay there until you delete it or until your computer shuts off.

Or on most modern operating systems, when the program ends.

[edited by - MaulingMonkey on June 1, 2004 11:04:42 PM]

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!