By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,408 Members | 945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,408 IT Pros & Developers. It's quick & easy.

Dynamic allocate memory problem

P: n/a
Hi All,

I use new to allocate some memory,even I doesn't use delete to release them.
When my Application exit, OS will release them.

Am I right?

If I'm right, how about Thread especally on Solaries OS?
This means that I use new to allocate memory in one Thread and doesn't use
delete to release them.
When this Thread dies, OS will release them?

Thank you so much!

PS: which book can I refer?

Franklin
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Franklin Lee wrote:
Hi All,

I use new to allocate some memory,even I doesn't use delete to release them.
When my Application exit, OS will release them.

Am I right?
No. It depends on your system. This is true for most modern
non-embedded operating systems.

If I'm right, how about Thread especally on Solaries OS?
This means that I use new to allocate memory in one Thread and doesn't use
delete to release them.
When this Thread dies, OS will release them?


No. Memory is shared between threads and solaris does not know which
thread allocated which memory.

Jul 22 '05 #2

P: n/a
Well, you "are right" , that is when your
application exits, the memory and other resources are automatically freed
by the operating system. However, this is NOT the important point....
Generally, in C++ you should dispose of memory you've allocated once you've
performed the operations for which you allocated it, that way you can reduce
the "high water mark" enabling you to handle larger amounts of data without
running out of virtual memory. Consider the situation such as a GUI like
MS
Word or Internet Exploder , you potentially could open, edit and close
multiple documents and have
the same application running for several days at a time. If memory wasn't
properly deallocated
once a document had been closed ( for instance), the app would eventually
consume all the virtual memory on the machine and would eventually crash, do
undefined things,
etc. Even with a simple "batch" type application, it is important to
identify the points in
the flow of code where allocated memory can be safely disposed, this way the
application
could potentially handle a larger data set with the same physical/virtual
resources available
on the computer.

Of course, if your app is sooooo simple that it just spits out some output
and quits, then this
is probably not an issue, although I would learn the habit of correctly
managing memory as
a professional skill to do as a matter of routine so you don't have to
revisit the code in the
future.

Threads have nothing to do with memory management (allocation/deallocation).
Memory can be allocated by one thread and deleted by another thread, there
are no requirements regarding disposing
of memory, you have to program that into your code. As a general rule in a
C++ program, wherever
you allocate memory using new ( or home brewed memory allocator), there must
be a corresponding
delete(s) somewhere to deallocate it, if you don't got that, you got a
memory leak. Whenever I see a
memory allocation such as malloc or new, I want to assure myself that there
is a correct delete/free
operation happening in the normal flow of control. Better still, be sure
there's a correct delete/free in
the C++ exception flow. Constructors/destructors are very handy for
correctly & automatically managing memory allocation/deallocation in C++
programs. Poor guys like me, had to do this
by pure brain-power before C++ came along, with C++ you've got it easy!

Tools such as Purify can help identify memory leaks after the fact, but in
my opinion its far
easier to bake the memory management into the original code. I'd add that
I've encountered a number of
"killer" bugs in production applications which were due to memory leaks, so
get into the
habit of correct memory management as a career advancement strategy. When
you allocate, figure
out where you need to deallocate.

I don't know which book you can look at, this is nothing to do with threads,
any good book
on C++ should drill in "initialization is resource allocation" and go from
there.

dave
"Franklin Lee" <pe*******@hotmail.com> wrote in message
news:c8********@netnews.proxy.lucent.com...
Hi All,

I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them.

Am I right?

If I'm right, how about Thread especally on Solaries OS?
This means that I use new to allocate memory in one Thread and doesn't use
delete to release them.
When this Thread dies, OS will release them?

Thank you so much!

PS: which book can I refer?

Franklin

Jul 22 '05 #3

P: n/a
Hi Gianni and ALL,

I believe that you are right.

But which books could I find the detail information?

Thank you so much!

Franklin

Jul 22 '05 #4

P: n/a
Franklin Lee posted:
Hi All,

I use new to allocate some memory,even I doesn't use delete to release
them. When my Application exit, OS will release them.

Am I right?

No disrepect to you, but you're like a child that plays with lego and then
leaves it thrown all over the room; but what do you care? - Mammy will clean
them up.

One thing though, in the C++ Standard, there's no guarantee that Mammy will
clean it up.
I suggest you put your hand in the fire and put a $1 in the "Leak Box" every
time you use "new" and then *don't* use "delete". Then rhyme this off to
yourself 100 times:
Where I use new , I must use delete
Where I use new , I must use delete
Where I use new , I must use delete
Where I use new , I must use delete
Where I use new , I must use delete
Where I use new , I must use delete
Where I use new , I must use delete
Where I use new , I must use delete

-JKop
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.