473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Dynamic allocate memory problem

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
4 2549
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Markus L?ffler | last post by:
Hi all, I'm looking for a class to access large memory blocks of dynamic length in an efficient way. Basically the simplest way to allocate a memory block is to allocate a byte . If you now...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
12
by: googlinggoogler | last post by:
Hi, Im new to C++ and trying to self teach myself whilst I sit at my unentertaining day job (thought i'd put that across before im accused of cheating on my homework, im 45...) Anyway I'm...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
18
by: welch.ryan | last post by:
Hi all, Having a problem with addressing large amounts of memory. I have a simple piece of code here that is meant to allocate a large piece of memory on a ppc64 machine. The code is: /*...
20
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()...
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.