473,563 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2571
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 "initializa tion is resource allocation" and go from
there.

dave
"Franklin Lee" <pe*******@hotm ail.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
2285
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 want that this memory block can grow dynamically, then you usually allocate a byte of a certain size, then if size is outgrown, you create a bigger...
6
8186
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 allocation to create object of that class ? 2. If it says "dynamic memory allocation", is it mean the following code : DestinationAddress* dest = new...
11
3372
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 don't know how many points there will be in every struct in the end, so I have to allocate memory dynamically for them and can't use an array of...
5
3742
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 believe the problem centers around my handling of strings, arrays, pointers and dynamic memory allocation. Here is the problem I'm trying to solve: ...
12
3053
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 trying to dynamically assign a structure whilst I read from a file, however my program crashes, and im not sure why other than that its to do with my...
1
7953
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 compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to...
18
2999
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: /* Test to see what happens when we try to allocate a massively huge piece of memory. */
20
6513
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() function in C.. But if I try to use a single linked list with a static char array fields I can free the memory allocated with out any problems using the...
10
4397
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 the allocation is impossible. Sworna vidhya
0
7579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7877
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6238
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5479
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
912
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.