Jump to content
  • Advertisement

Archived

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

elis-cool

Is there even a point?

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

Is there even a point to using delete to delete memory you''ve used from the heap when the compiler deletes it anyway when your program ends?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
1) the compiler almost certainly doesn''t deallocate the memory (unless in debug mode)

2) if you deallocate the memory it will be available again so you''re not wasting the resources the operating system has given you to use

pete (no login sorry)



Share this post


Link to post
Share on other sites
It depends on what your program does. If, for example, you had written a process that allocates resources during it's lifetime, runs for a few seconds and then terminates, then there's little point other than observing good programming practice. Most OS's will clean-up after a process regardless of how it terminates.

On the other hand, if you have a program that runs for a long time, allocating resources and not freeing them, it's easy to imagine how that will cause a problem. Increasing amounts of memory will be used until either your virtual memory system gets a good thrashing or you run out of memory entirely.

A point worthy of note is that there is far less utility in using a "bald" pointer than most C++ programmers seem to think. Lots of uses of pointers can be replaced with smart pointers, handle classes and the like, often removing the need to carry out manual memory management.

--

It is against the law to stare at the mayor of Paris.


Edited by - SabreMan on February 26, 2002 5:32:01 AM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
actually also if your objects aquire resources from the OS (not just memory) like locks, COM interfaces, network connections etc and you don''t release them the OS won''t ''clean up'' after you

pete (sorry still no login)

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
A point worthy of note is that there is far less utility in using a "bald" pointer than most C++ programmers seem to think. Lots of uses of pointers can be replaced with smart pointers, handle classes and the like, often removing the need to carry out manual memory management.

It was worth repeating, and leads me to...

quote:
Orignal post by AP pete
actually also if your objects aquire resources from the OS (not just memory) like locks, COM interfaces, network connections etc and you don''t release them the OS won''t ''clean up'' after you

CComPtr is a wonderful template class that manages COM interfaces for you. Note that you don''t need to explicitly access the controlled pointer; all your methods can be called as if the CComPtr was a naked COM interface:

CComPtr Direct3D = 0;
Direct3D.Attach( ::Direct3DCreate8( D3D_SDK_VERSION ) );
CComPtr D3DDevice = 0;
Direct3D->CreateDevice(..., &D3DDevice);
// etc

I''m looking at using std::auto_ptr or the smart pointer from Boost to simplify my own pointer management.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Hi, I''m proficient in c++ as a language, but what is a smart pointer exactly. And what is STL - just a standard library?

btw, does windows not automatically deallocate all resources when your app exits, including handles and such? If not, why is such a crummy OS so popular - I could write code to manage such things.

Share this post


Link to post
Share on other sites
Win9x/ME does a poor job of cleaning up after you application's allocate memory. WinNT derivatives are better, but they still aren't perfect under certain circumstances (they don't always clean up if you application crashes instead of closing normally, I've noticed). Most any other OS does a perfect job when you application closes normally, however their responsed under other conditions vary per OS. However, like the previous posters have said, as long as your application is running, the memory used just keeps building up until the system runs extremely slowly (VM thrashing) and/or dies (locks up). Editted: To make it sound more like what I meant to say.



Edited by - Null and Void on March 4, 2002 11:08:38 PM

Share this post


Link to post
Share on other sites
quote:
Original post by d000hg
...what is a smart pointer exactly?

A smart pointer is a pointer that "knows" when it goes out of scope and as such it deallocates itself. It''s usually implemented as a template class:

  
template <typename Type>
class auto_pointer
{
public:
auto_pointer();
~auto_pointer();
inline Type * operator -> ();
//

private:
Type * pointer; // the controlled "actual" pointer

};
//

template <typename Type>
auto_pointer<Type>::auto_pointer()
{
pointer = new Type;
}
template <typename Type>
auto_pointer<Type>::~auto_pointer()
{
delete pointer;
}
template <typename Type>
Type * auto_pointer<Type>::operator -> ()
{
return pointer;
}

The reason operator -> behaves as expected is because its semantics "cascade" downwards until a non-object attribute or operation is found.

quote:
...what is STL - just a standard library?

The Standard Template Library is a part of the Standard C++ Library since the 1998 C++ Standard (which was apparently actually finalized in 1997, but I digress) that provides generic Containers, Iterators and Algorithms for a variety of programming tasks (I hear for this reason one of the suggested names was CIA).

Containers are data structures that control sequences of objects/data, freeing C++ programmers from having to implement them over and over. Common containers (data structures most people are familiar with) include vectors (arrays), stacks, lists, deques (double-ended queues) and queues, maps and multimaps, and sets and multisets. Some STL implementations include hash maps and hash sets, but they''re not currently part of the standard (they''re under consideration for the next revision though).

Some containers are actually adapters of other containers to provide the required semantics.

Iterators are special, pointer-like objects designed to allow sequential access to the sequences controlled by Containers. While a vector (array) allows direct, constant-time access to its data, a list requires iteration (thus the name) from the head to the desired element. Being pointer like, the iterators can be incremented to move to the next element and so forth, and dereferenced to provide access to the actual value.

Algorithms are general-purpose, well, algorithms that everybody needs - sorting, for example. There''s also algorithms like for_each that simplify structuring your own algorithms.

The STL links in my and Kylotan''s signatures point you to the excellent SGI documentation (including design considerations for the STL itself). Check it out for a much more exhaustive explanation as well as a complete reference for every container, iterator and algorithm.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
It's amazing how many lazy-ass kids have been asking that question within the last few weeks. Wipe your own ass like a big boy—deallocate your own resources.

To anyone using a non-garbage-collected language (such as C and C++) who's even inclined to assume that some entity will take care of deallocation for you:
- Switch to a garbage-collected language
- Stop programming altogether, and stop flooding the world with even more buggy, leaky software

(Oh, and a compiler can't deallocate memory for your program. Its a compiler, not an execution environment)

Edited by - merlin9x9 on March 4, 2002 12:36:51 AM

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!