Freeing memory after malloc() - quick reply please! 
Author Message
 Freeing memory after malloc() - quick reply please!

Hi, I am studying C in the uni.
I our project we had to use pointers with linked list.
My question is: Is the memory returned to the system upon program exit, or
I have to delete the nodes and use free() to return it?
I know, that if I delete a node, I am supposed to free the memory allocated
to it, for if I exit from the function I was when using, unlike with the
local variables, the memory allocated to them are not returned.
Is it so with program exit? If I don't delete the nodes and free the
memory, does it cause memory leak (or loss - I am not sure how you call it)?

Please, reply, as I'd like to correct my program and lodge it tomorrow.

Regards,  Bela Lantos



Mon, 27 Oct 2003 05:14:55 GMT  
 Freeing memory after malloc() - quick reply please!
Quote:

> Hi, I am studying C in the uni.
> I our project we had to use pointers with linked list.
> My question is: Is the memory returned to the system upon program exit, or
> I have to delete the nodes and use free() to return it?

Depends on the OS. I am sure about Unices that they always reclaim memory,
i.e. you DON'T have to explicitly free all the memory before program exit.
I also think that MSDOS didn't reclaim memory... Here's a sample:

#include <stdlib.h>
int main() {
        malloc(65536); /* allocate 64k */
        return 0;

Quote:
}

On Unices it will NEVER exhaust system memory. On MSDOS I think it will.
For winbloze I don't know.

Anyway, if you're running a decent OS, you shouldn't have to bother freeing
allocated memory just before program exit.



Sun, 26 Oct 2003 20:31:30 GMT  
 Freeing memory after malloc() - quick reply please!

Quote:


> > Hi, I am studying C in the uni.
> > I our project we had to use pointers with linked list.
> > My question is: Is the memory returned to the system upon program exit, or
> > I have to delete the nodes and use free() to return it?

[snip]

Quote:

> On Unices it will NEVER exhaust system memory. On MSDOS I think it will.
> For winbloze I don't know.

> Anyway, if you're running a decent OS, you shouldn't have to bother freeing
> allocated memory just before program exit.

However, it's bad practice not to. See the current thread 'free in a
function'.
--
Owen Brydon


Sun, 26 Oct 2003 21:48:20 GMT  
 Freeing memory after malloc() - quick reply please!

Quote:

> Hi, I am studying C in the uni.
> I our project we had to use pointers with linked list.
> My question is: Is the memory returned to the system upon program exit, or
> I have to delete the nodes and use free() to return it?
> I know, that if I delete a node, I am supposed to free the memory allocated
> to it, for if I exit from the function I was when using, unlike with the
> local variables, the memory allocated to them are not returned.
> Is it so with program exit? If I don't delete the nodes and free the
> memory, does it cause memory leak (or loss - I am not sure how you call it)?

Depends on what you are doing.  It sounds as if you are building
generic linked list handlers, such as insertnode, deletenode,
unlinknode, findnode, sortlist, etc.  In general you should free
the memory as soon as you are done with it.  Thus deletenode
should probably free, unlinknode should probably not, etc.

Whenever you are about to lose the pointer to some malloced
memory, it should first be freed.

When the program is all done, the fact that you have a list of
linked nodes still lying about makes no real difference, the opsys
will (should) recapture it all.

--

http://www.qwikpages.com/backstreets/cbfalconer :=(down for now)
   (Remove "NOSPAM." from reply address. my-deja works unmodified)



Sun, 26 Oct 2003 22:00:15 GMT  
 Freeing memory after malloc() - quick reply please!

Quote:

>> Hi, I am studying C in the uni.
>> I our project we had to use pointers with linked list.
>> My question is: Is the memory returned to the system upon program exit, or

Yes, on all the systems I'm aware of.  When the OS cannot reclaim this
memory itself, it is the C runtime environment that takes care of this
job.

Quote:
>> I have to delete the nodes and use free() to return it?

That's more a question of style than a technical one.

Quote:
>Depends on the OS. I am sure about Unices that they always reclaim memory,
>i.e. you DON'T have to explicitly free all the memory before program exit.
>I also think that MSDOS didn't reclaim memory... Here's a sample:

>#include <stdlib.h>
>int main() {
>    malloc(65536); /* allocate 64k */
>    return 0;
>}

>On Unices it will NEVER exhaust system memory. On MSDOS I think it will.

Nope, it won't.  In MSDOS, the application owns all the available memory
below the 640k limit.  It is therefore trivial for the OS to reclaim it
upon program termination.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Sun, 26 Oct 2003 22:55:38 GMT  
 Freeing memory after malloc() - quick reply please!

Quote:
>Hi, I am studying C in the uni.
>I our project we had to use pointers with linked list.
>My question is: Is the memory returned to the system upon program exit, or
>I have to delete the nodes and use free() to return it?
>I know, that if I delete a node, I am supposed to free the memory allocated
>to it, for if I exit from the function I was when using, unlike with the
>local variables, the memory allocated to them are not returned.
>Is it so with program exit?

No.

Quote:
>If I don't delete the nodes and free the
>memory, does it cause memory leak (or loss - I am not sure how you call it)?

No.  A memory leak is when you lose the address of a dynamically allocated
memory block, so you are no longer able to free it, even if you no longer
need it.  Program termination does not cause memory leaks, because it is
the implementation (either the run time C environment or the OS itself)
that takes care of releasing *all* the resources allocated to the
program.

Quote:
>Please, reply, as I'd like to correct my program and lodge it tomorrow.

In your case, it's better to free everything before exiting, even if
not technically needed, because some people religiously believe that
you should do so.  Your instructor might be one of them :-)

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Sun, 26 Oct 2003 23:05:24 GMT  
 Freeing memory after malloc() - quick reply please!

Quote:


> > Hi, I am studying C in the uni.
> > I our project we had to use pointers with linked list.
> > My question is: Is the memory returned to the system upon program exit, or
> > I have to delete the nodes and use free() to return it?

> Depends on the OS. I am sure about Unices that they always reclaim memory,
> i.e. you DON'T have to explicitly free all the memory before program exit.
> I also think that MSDOS didn't reclaim memory... Here's a sample:

> #include <stdlib.h>
> int main() {
>         malloc(65536); /* allocate 64k */
>         return 0;
> }

> On Unices it will NEVER exhaust system memory. On MSDOS I think it will.
> For winbloze I don't know.

> Anyway, if you're running a decent OS, you shouldn't have to bother freeing
> allocated memory just before program exit.

On MsDos, 65536 % 65536 is very close to 0.  Usually the malloc
limit is 65520, to allow space for the arena proper.  Doxen from
3.3 up, at least, clean up properly.

I believe the maximum guaranteed object size is 32767.  Anything
beyond this is either undefined or implementation defined.  I
don't think there is any value in limits.h for it, but if I am
wrong we will hear about it.

--

http://www.qwikpages.com/backstreets/cbfalconer :=(down for now)
   (Remove "NOSPAM." from reply address. my-deja works unmodified)



Sun, 26 Oct 2003 23:16:59 GMT  
 Freeing memory after malloc() - quick reply please!

Quote:

>> #include <stdlib.h>
>> int main() {
>>         malloc(65536); /* allocate 64k */
>>         return 0;
>> }

>On MsDos, 65536 % 65536 is very close to 0.

size_t needs not be a 16-bit type on MSDOS implementations.

Quote:
>Usually the malloc
>limit is 65520, to allow space for the arena proper.

The MSDOS heap needs not be limited to 64k.

Both issues are controlled by the implementation, not by MSDOS.

Quote:
>Doxen from 3.3 up, at least, clean up properly.

It's a trivial job, anyway.

Quote:
>I believe the maximum guaranteed object size is 32767.

True (for C89) and irrelevant in context.  What is a bit more relevant
is the actual definition of size_t: depending on that we can figure out
how much memory the program actually attempts to allocate.  On typical
MSDOS implementations it will be 0 bytes.

Quote:
>Anything beyond this is either undefined or implementation defined.

However, since we are dealing with a dynamical allocation issue and
since its success or failure does not affect the program's output, the
program in question is still a strictly conforming one.

Quote:
>I don't think there is any value in limits.h for it, but if I am
>wrong we will hear about it.

The implementation is not required to document this value in any way.
It's not even required to guarantee that each and every program or even
that each and every run of the same program can always succeed in
allocating (statically, dynamically or automatically) an object of the
minimum "guaranteed" maximum size (32767 in C89, 65535 in C99, for hosted
implementations only).

Think of an MSDOS system with only 48k of memory available for running
applications.  This can be trivially achieved by creating a ramdisk of
the "right" size in conventional memory.  Trivial programs may still be
able to allocate a 32767 bytes object, more complex ones won't.  Increase
the size of the ramdisk to have a system with only 30k of free memory
left for applications and you have rendered your C implementation
non-conforming!

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Mon, 27 Oct 2003 00:13:06 GMT  
 Freeing memory after malloc() - quick reply please!

Quote:
> Hi, I am studying C in the uni.
> I our project we had to use pointers with linked list.
> My question is: Is the memory returned to the system upon program exit, or
> I have to delete the nodes and use free() to return it?
> I know, that if I delete a node, I am supposed to free the memory allocated
> to it, for if I exit from the function I was when using, unlike with the
> local variables, the memory allocated to them are not returned.
> Is it so with program exit? If I don't delete the nodes and free the
> memory, does it cause memory leak (or loss - I am not sure how you call it)?

> Please, reply, as I'd like to correct my program and lodge it tomorrow.

From the C FAQ:

7.22:   When I call malloc() to allocate memory for a pointer which is
        local to a function, do I have to explicitly free() it?

A:      Yes.  Remember that a pointer is different from what it points
        to.  Local variables are deallocated when the function returns,
        but in the case of a pointer variable, this means that the
        pointer is deallocated, *not* what it points to.  Memory
        allocated with malloc() always persists until you explicitly
        free it.  In general, for every call to malloc(), there should
        be a corresponding call to free().

And also:

7.24:   Must I free allocated memory before the program exits?

A:      You shouldn't have to.  A real operating system definitively
        reclaims all memory and other resources when a program exits.
        Nevertheless, some personal computers are said not to reliably
        recover memory, and all that can be inferred from the ANSI/ISO C
        Standard is that this is a "quality of implementation issue."

        References: ISO Sec. 7.10.3.2.

On the other hand, it's bone-lazy not to, and shows a lack of discipline.  I
always free everything I allocate.  We use memory de{*filter*}s that will track
leaks for us.  If I just let some memory get reclaimed automatically by the
system, it would make a big pile of debugging messages and mask any real
leakage messages.  In my opinion, you should free it.  Have you read the C FAQ
yet?
--
C-FAQ: http://www.*-*-*.com/ ~scs/C-faq/top.html
 "The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. FAQ: ftp://cap.connx.com/pub/Chess%20Analysis%20Project%20FAQ.htm



Mon, 27 Oct 2003 00:49:56 GMT  
 Freeing memory after malloc() - quick reply please!

Quote:

... snip ...

> Nope, it won't.  In MSDOS, the application owns all the available memory
> below the 640k limit.  It is therefore trivial for the OS to reclaim it
> upon program termination.

Not necessarily.  COM files get allocated everything, and have to
do some specific cut-backs to function as TSR's, etc.  EXE files
have a field for data space allocated, and don't necessarily have
to trim themselves.  So they can do things like shell, execute
other programs, etc. It is a different world.

You can create all sorts of havoc by executing a TSR from a
program, and then exiting that program.  Also bringing down TSRs
in the wrong order.  Nobody worries about those things any more.

--

http://www.qwikpages.com/backstreets/cbfalconer :=(down for now)
   (Remove "NOSPAM." from reply address. my-deja works unmodified)



Mon, 27 Oct 2003 00:54:10 GMT  
 Freeing memory after malloc() - quick reply please!
: I our project we had to use pointers with linked list.
: My question is: Is the memory returned to the system upon program exit, or
: I have to delete the nodes and use free() to return it?

Your question is similar to question 7.24 of the comp.lang.c FAQ,
"Must I free allocated memory before the program exits?"

The short answer is:  "You shouldn't have to."  However the FAQ goes
into some detail on this topic, and I suggest you read the answer to
this question.  The URL of the comp.lang.c FAQ can be found in the posts
of Jack Klein and other prolific regular posters to this group.

Paul

--
Paul D. Boyle

North Carolina State University
http://laue.chem.ncsu.edu/web/xray.welcome.html



Sun, 26 Oct 2003 20:37:32 GMT  
 Freeing memory after malloc() - quick reply please!

Quote:

>... snip ...

>> Nope, it won't.  In MSDOS, the application owns all the available memory
>> below the 640k limit.  It is therefore trivial for the OS to reclaim it
>> upon program termination.

>Not necessarily.  COM files get allocated everything, and have to
>do some specific cut-backs to function as TSR's, etc.  EXE files
>have a field for data space allocated, and don't necessarily have
>to trim themselves.

And the typical value you're going to find in this field is 0xffff, which
means all the available conventional memory.  This allows them to use the
"far heap", whose size is limited only by the available conventional
memory.

Anyway, the OS knows how much memory was allocated to that program and it
is trivial to reclaim it upon program termination.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Mon, 27 Oct 2003 01:25:05 GMT  
 Freeing memory after malloc() - quick reply please!
Bela Lantos a crit dans le message

Quote:
>Hi, I am studying C in the uni.
>I our project we had to use pointers with linked list.
>My question is: Is the memory returned to the system upon program exit,
or
>I have to delete the nodes and use free() to return it?
>I know, that if I delete a node, I am supposed to free the memory
allocated
>to it, for if I exit from the function I was when using, unlike with
the
>local variables, the memory allocated to them are not returned.
>Is it so with program exit? If I don't delete the nodes and free the
>memory, does it cause memory leak (or loss - I am not sure how you call

it)?

I suggest you always free the memory you have allocated. Don't rely on
the system to clean after you. Some do it, some don't. Also, some
programs are designed to run day and night. You can be sure that if such
a program didn't care about memory management, it will crash one day or
another.

Quote:
>Please, reply, as I'd like to correct my program and lodge it tomorrow.

Huh! In that case, better to do nothing, or to ask for a new deadline.

--
-hs-    "spaces, not tabs" email: emdel at noos.fr
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://secure.dinkumware.com/htm_cl/
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Mon, 27 Oct 2003 04:00:25 GMT  
 Freeing memory after malloc() - quick reply please!

Quote:
>I suggest you always free the memory you have allocated. Don't rely on
>the system to clean after you. Some do it, some don't. Also, some

                                            ^^^^^^^^^^
Name a couple that don't.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Mon, 27 Oct 2003 05:18:18 GMT  
 Freeing memory after malloc() - quick reply please!


|
|>I suggest you always free the memory you have allocated. Don't rely on
|>the system to clean after you. Some do it, some don't. Also, some
|                                            ^^^^^^^^^^
|Name a couple that don't.

DS 9000
DS 9000a

--
-hs-    "spaces, not tabs" email: emdel at noos.fr
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://secure.dinkumware.com/htm_cl/
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Mon, 27 Oct 2003 05:49:10 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. custom malloc()free() to record memory usage?

2. newbie malloc problem: free memory allocated to structures

3. Memory Leakage problem using malloc and free

4. freeing only part of malloc-ed memory?

5. Freeing malloc-ed memory - question

6. dynamic memory allocation performance problems with malloc/free

7. Malloc not freeing memory

8. memory allocation using malloc and free

9. Error "free"-ing "malloc"-ed memory

10. stack based malloc/free (feedback please)

11. please help on malloc/free!

12. Please help: malloc() and free()

 

 
Powered by phpBB® Forum Software