473,508 Members | 4,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a simple problem about memory allocation


excuse me!!

may i ask a simple problem here?

if i dynamically allocate a memory(ex. new in C++ or malloc in C)

in a sub-function and forget free the space end of the sub-function.

does it cause memory leak or the space automatically free by OS??

thx...

--
>> ¥xÆW¬ì¤j¸ê¤u¨t ²M¬y¯¸ # ntust.org #
>> Author from: dcs9.ee.ntust.edu.tw 
Nov 13 '05 #1
6 2500
Nick <ni******@ntust.org> scribbled the following:
excuse me!! may i ask a simple problem here? if i dynamically allocate a memory(ex. new in C++ or malloc in C)
C++ is off-topic here. C and malloc are on-topic though.
in a sub-function and forget free the space end of the sub-function. does it cause memory leak or the space automatically free by OS??
It causes a memory leak, unless you save the pointer in some other
variable and free the space afterwards in some other function.
thx...


Ur wlcm.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It's not survival of the fattest, it's survival of the fittest."
- Ludvig von Drake
Nov 13 '05 #2
ni******@ntust.org (Nick) wrote:
if i dynamically allocate a memory(ex. new in C++ or malloc in C)
in a sub-function and forget free the space end of the sub-function.
does it cause memory leak or the space automatically free by OS??


That depends on the implementation. The vast majority of them will free
your memory for you when the program ends. However, since the calling OS
is beyond the scope of the Standard, this is not guaranteed.
It's still much better practice to clean up after yourself, of course,
if only because it's easier to maintain.

Richard
Nov 13 '05 #3
you loose that part of memory. as a good practice i recomend after you write
a code snipet for memory allocation imediatly after that write code snipet
to release it. always write these commands as pairs. than all the other code
write between those two code snipets.
Nov 13 '05 #4
Mac
On Fri, 07 Nov 2003 13:06:06 +0000, Nick wrote:

excuse me!!

may i ask a simple problem here?

if i dynamically allocate a memory(ex. new in C++ or malloc in C)

in a sub-function and forget free the space end of the sub-function.

does it cause memory leak or the space automatically free by OS??

thx...

There are really two cases to consider.

1) You use some memory, and you are not done with it until the program
terminates. Some people in this news group argue that in this case
it is OK to not free the memory, since the OS will reclaim it anyway.

2) Case 2 is different. In this case, you throw away your pointer to the
malloc'd memory without freeing it. This is a memory leak, and is always
wrong. Here is an example:

ptr = malloc(buff);

....

ptr = malloc(buff *= 2); /* memory leak here */
In the above example, ptr must be either freed or stored somewhere else
before the second call to malloc(). If you neither free it, nor save it,
then you lose the ability to free it, even if you want to.

HTH

Mac
--

Nov 13 '05 #5
On Fri, 07 Nov 2003 07:40:27 -0500, Richard Bos wrote:
ni******@ntust.org (Nick) wrote:
if i dynamically allocate a memory(ex. new in C++ or malloc in C) in
a sub-function and forget free the space end of the sub-function.
does it cause memory leak or the space automatically free by OS??


That depends on the implementation. The vast majority of them will free
your memory for you when the program ends. However, since the calling OS
is beyond the scope of the Standard, this is not guaranteed. It's still
much better practice to clean up after yourself, of course, if only
because it's easier to maintain.

Richard


It's my understanding that the OS leaves the memory mapped to the
programs heap(or storage pool) and then draws from that pool for any new
malloc calls.

It's my understanding that the sequence works like this mmap() malloc()
[mlock() | munlock()] free() munmap()

So the only way to garauntee that the memory is reclaimed by the OS is to
unmap it after calling free().

Yes?

Freejack
Nov 13 '05 #6
On Sun, 09 Nov 2003 07:45:43 GMT
Freejack <us**@nospam.net> wrote:

<snip>
It's my understanding that the OS leaves the memory mapped to the
programs heap(or storage pool) and then draws from that pool for any
new malloc calls.
That is a common way to implement it, however it is not required to do
that.
It's my understanding that the sequence works like this mmap()
malloc()[mlock() | munlock()] free() munmap()

So the only way to garauntee that the memory is reclaimed by the OS is
to unmap it after calling free().


mmap, mlock, munlock and munmap are off topic since that are not part of
ISO C, however...

If you are using malloc to obtain memory then it won't be obtained from
memory *you* have mmap'ed. If you munmap memory that was malloced then
freed you are quite likely to crash you program since if it was not
mmap'ed (you don't know whether it was) that it is probably an invalid
operation and if it was mmap'ed then because you have gone behind the
implementations back it may well return you a pointer to the memory you
unmap'ed without having re-obtained the memory from the OS.

In other words, DON'T mix the system specific calls and the ISO C
library calls on the same objects.

I would suggest using malloc/free unless you have performance issues
which a profiler shows are due to the overhead of malloc/free, then
consider whether you can use malloc/free in a more efficient manner (say
by mallocing memory in larger chunks) and only as a last resort change
to using system specific calls.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3232
by: Tom | last post by:
We have a VERY simple .NET C# Form Application, that has about a 23MB Memory Footprint. It starts a window runs a process and does a regular expression. I have done a GC.Collect to make sure that,...
11
2676
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
8
1557
by: jason | last post by:
i'm a little new to VC++, so i'm curious how to appropriate perform the following task. there is a pointer which i wish you point to a buffer of frequently changing size. i'm wondering what is...
13
5710
by: Michael B Allen | last post by:
Hi, I've tried to write the *simplest* memory allocator possible. I think it would be useful in many cases such as allocating memory on stack as a poor man's garbage collection perhaps. I was...
18
2423
by: Peter Smithson | last post by:
Hi, I've read this page - http://devrsrc1.external.hp.com/STK/impacts/i634.html but don't understand it. Here's the text - "Non-standard usage of setjmp() and longjmp() could result in...
8
5094
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
1
1452
by: kiplring | last post by:
List<string> effectList = new List<string>(); effectList.Clear(); effectList = null; using (List<string> effectList = new List<string>()) { } If there are so many calls, I should save as...
24
19037
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
10
1795
by: Sourcerer | last post by:
I wrote this very simple code in .NET VC++. I compiled it on my system, and tried to run it on my friend's computer (he doesn't have the compiler). We both have Windows XP Professional. I have .NET...
8
3156
by: Chris M. Thomasson | last post by:
Here is the initial crude implmentation which compiles under Comeau with no warnings: ___________________________________________________________________ #include <cassert> #include <cstdlib>...
0
7128
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...
0
7393
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...
1
7058
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4715
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...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
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...

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.