473,249 Members | 1,902 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,249 software developers and data experts.

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...

--
>> xWjut My # ntust.org #
>> Author from: dcs9.ee.ntust.edu.tw 
Nov 13 '05 #1
6 2488
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
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
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
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
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
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
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
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
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
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
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
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.