Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Malloc/Free - freeing memory allocated by malloc

Reply
Thread Tools

Malloc/Free - freeing memory allocated by malloc

 
 
Peter
Guest
Posts: n/a
 
      09-20-2004
Is it really necessary to call free()? Can I make the assumption that
the OS will do the clean up once the program terminates?

For a linked list, I can step through the list and free one node at a
time, but what about a dynamic array created by malloc?

int *data = malloc(100 * sizeof(int));

Does free(data) frees all 100 elements or only the first one? Do I
have to do the following in a loop?

free(data[0]);
free(data[1]);
free(data[2]);
...
...

I appreciate your input.

Peter
 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      09-20-2004
Peter <> wrote:
> Is it really necessary to call free()? Can I make the assumption that
> the OS will do the clean up once the program terminates?


On most OS's you can, but if that's a good idea is another question,
especially if the code may become part of a larger program later on
(so what's now main() becomes a function of that larger program).

> For a linked list, I can step through the list and free one node at a
> time, but what about a dynamic array created by malloc?


> int *data = malloc(100 * sizeof(int));


> Does free(data) frees all 100 elements or only the first one? Do I
> have to do the following in a loop?


It frees all of them. You must call free() exactly once for each time
you called malloc() (or calloc() etc.). You are only allowed to use
the pointer as you received it from malloc() in the call of free(),
nothing else.
Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
 
Gordon Burditt
Guest
Posts: n/a
 
      09-20-2004
>Is it really necessary to call free()? Can I make the assumption that
>the OS will do the clean up once the program terminates?


You cannot make the assumption that the program is supposed to
terminate. Daemons don't. (and code that is reusable may be
incorporated into a daemon.) Sloppy programming with memory leaks
is likely to result in a system that has to be rebooted frequently
just to get back leaked memory.

>For a linked list, I can step through the list and free one node at a
>time, but what about a dynamic array created by malloc?


One malloc, one free. Many mallocs, many frees.

> int *data = malloc(100 * sizeof(int));
>
>Does free(data) frees all 100 elements or only the first one? Do I


If you got it in one malloc, all you need is one free.
malloc() does NOT know how you are using the memory.

>have to do the following in a loop?
>
> free(data[0]);
> free(data[1]);
> free(data[2]);
> ...
> ...


free() takes a pointer as an argument. You are feeding it an integer
argument here.

Gordon L. Burditt
 
Reply With Quote
 
Jason Curl
Guest
Posts: n/a
 
      09-20-2004
wrote:
> Peter <> wrote:
>
>>Is it really necessary to call free()? Can I make the assumption that
>>the OS will do the clean up once the program terminates?

>
>
> On most OS's you can, but if that's a good idea is another question,
> especially if the code may become part of a larger program later on
> (so what's now main() becomes a function of that larger program).
>


I remember for the AmigaOS, it didn't reclaim lost memory. It didn't use
an MMU. So, it's not really a good idea to assume the OS will clean it
up for you.

>
>>For a linked list, I can step through the list and free one node at a
>>time, but what about a dynamic array created by malloc?

>
>
>> int *data = malloc(100 * sizeof(int));

>
>
>>Does free(data) frees all 100 elements or only the first one? Do I
>>have to do the following in a loop?

>
>
> It frees all of them. You must call free() exactly once for each time
> you called malloc() (or calloc() etc.). You are only allowed to use
> the pointer as you received it from malloc() in the call of free(),
> nothing else.
> Regards, Jens

 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      09-20-2004
(Peter) wrote:
# Is it really necessary to call free()? Can I make the assumption that
# the OS will do the clean up once the program terminates?

It depends on the OS. You have to check your OS documentation to find out.

I think it's a good idea in most cases. If someday you or someone else wants to take
your entire program and change it into a package loaded into another program, doing
complete memory management in the first place is a big help.

# For a linked list, I can step through the list and free one node at a
# time, but what about a dynamic array created by malloc?
#
# int *data = malloc(100 * sizeof(int));

You should have one free for each malloc and for the last pointer returned by realloc.
malloc probably is unaware whether you want a very large struct or a hundred elements;
it returns one block for each call, and it is the block that has to be freed.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
JUSTICE!
Justice is dead.
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      09-20-2004

"Peter" <> wrote
>
> Is it really necessary to call free()? Can I make the assumption that
> the OS will do the clean up once the program terminates?
>

No decent OS will allow normal applications to hog memory, even after being
terminated.
However inability to free your memory on program exit is usually a sign that
you don't have memory management under control. In particular, how will you
find genuine memory leaks?


 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      09-21-2004
Thanks everyone for replying, I will definitely "free" after each
"malloc". Something strange is that after calls to "free", the linked
list is still accessible, the values are still there. Should I also
assign NULL value to each node?


(Peter) wrote in message news:<. com>...
> Is it really necessary to call free()? Can I make the assumption that
> the OS will do the clean up once the program terminates?
>
> For a linked list, I can step through the list and free one node at a
> time, but what about a dynamic array created by malloc?
>
> int *data = malloc(100 * sizeof(int));
>
> Does free(data) frees all 100 elements or only the first one? Do I
> have to do the following in a loop?
>
> free(data[0]);
> free(data[1]);
> free(data[2]);
> ...
> ...
>
> I appreciate your input.
>
> Peter

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      09-21-2004
Peter wrote:
>
> Thanks everyone for replying, I will definitely "free" after each
> "malloc". Something strange is that after calls to "free", the linked
> list is still accessible, the values are still there.


Any test of a freed pointer invokes undefined behavior.
It may be that the memory was simply not yet reallocated
by the time that you checked it, but by checking it,
your program was still using memory that it didn't own.

> Should I also
> assign NULL value to each node?


I wouldn't.

This is what I use to free a list:

void list_free(list_type *node, void (*free_data)(void *))
{
list_type *next;

while (node != NULL) {
free_data(node -> data);
next = node -> next;
free(node);
node = next;
}
}

where list_type is defined as:

#define LIST_TYPE \
struct list_node { \
struct list_node *next; \
void *data; \
}

typedef LIST_TYPE list_type;

--
pete
 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      09-21-2004
Peter <> wrote:
> Thanks everyone for replying, I will definitely "free" after each
> "malloc". Something strange is that after calls to "free", the linked
> list is still accessible, the values are still there. Should I also
> assign NULL value to each node?


Memory may still be accessible via the pointer and it may even still
hold what it did before you called free(). But that's by pure chance,
what the pointer was pointing to may also have become inaccessible or
the memory it's pointing to may contain some other data. You simply
can't know and using the pointer after free() invokes undefined beha-
vior, i.e. everything can happen. So be very careful never to do that.

If setting the pointer to NULL after the call of free() helps you in
not using the pointer anymore then set it to NULL - but you don't
have to zero out the memory before free()ing it, that would just be
a waste of time. Setting the pointer to NULL is not required - all
you must do is refrain from using that pointer for anything at all
(including even just looking at its value).

int *x = malloc( 100 * sizeof *x );
....
free( x );

Both the following lines would invoke undefined behavior.

printf( "%d %d\n", x[ 11 ], *( x + 42 ); /* wrong! */
printf( "%p\n", ( void * ) x ); /* wrong! */

even though on some machines you might get away with doing that. All
you're allowed to do with 'x' after calling free() on it is to assign
a new value to it, e.g. by using a further call of malloc()

x = mallod( 20 * sizeof *x );

or setting it to NULL
Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________ http://www.toerring.de
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      09-21-2004
In < > (Peter) writes:

>Thanks everyone for replying, I will definitely "free" after each
>"malloc". Something strange is that after calls to "free", the linked
>list is still accessible, the values are still there.


If you can still access those addresses, it means that the freed memory
still belongs to your program (it is available for further allocation).
The free() function typically doesn't touch the contents of the memory it
deallocates.

However, there are implementations where, under certain conditions, the
deallocated memory no longer belongs to the program. Trying to access
the freed memory will crash your program.

>Should I also assign NULL value to each node?


What for? If you're concerned about security issues (you don't want your
data to be available to whatever program might "inherit" the memory you
have freed), don't worry: the operating system is supposed to clear the
memory before allocating it to another program.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email:
Currently looking for a job in the European Union
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to check whether malloc has allocated memory properly in caseif malloc(0) can return valid pointer Gene C Programming 0 12-20-2010 05:33 AM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
freeing allocated memory Curley Q. C Programming 7 04-30-2004 04:09 PM
freeing allocated memory binaya C Programming 11 10-19-2003 06:44 PM
some queries on freeing memory allocated using malloc Hassan Iqbal C Programming 3 09-25-2003 02:53 PM



Advertisments