LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-08-2006, 05:01 PM   #1
pentalive
Member
 
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 124

Rep: Reputation: 15
Question about malloc - free and exit prog.


I am writing a tiny basic-like language, for each statement the user types in (or read from a save file) some memory is malloc'ed.

Before my program ends do I need to free each one? Is there a call to free all of them at once? Does Linux recover the memory if I don't free it?

I will also need to clear all these malloc'd bits if the user clears the program space (to enter a new program), so a "clear every malloc" would still be nice.

Thanks!
 
Old 05-08-2006, 05:23 PM   #2
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
If i remember correctly when a program ends its address space is marked as avalible and can be reused by another program so in essence yes its freed at program termination. But still its a good idea to free all memory you used for good mesure.
 
Old 05-08-2006, 05:33 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
When you exit all the allocated memory will be automatically free'd for you, otherwise you have to free it all individually. Which isn't that hard, you can keep track of the allocated memory by constructing a linked list of the variables that you have allocated.

An alternative way to tackle it would be to run the user program as a child process. That way it would be isolated from the rest of the program and so any "crash" situation wouldn't actually crash the parent process.

Last edited by graemef; 05-08-2006 at 05:36 PM.
 
Old 05-08-2006, 06:10 PM   #4
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 34
It's going to be difficult (that is, impossible) to find memory leaks if you don't free your memory after you've finished with it. For every function that allocates memory you should have a cleanup function that frees it again. In between, you keep track of the pointers using some sort of data structure (list/hash table/etc), as graemef suggests.
 
Old 05-08-2006, 06:32 PM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
A good way(for me) to make sure that I've freed everything I've malloc is to grep my sources and make sure that every ?alloc has an associated free.

For this thing, I like the idea of an array of pointers for each procedure. Free'em when the procedure finishes. Assuming your program is going to support some form of functions and not just one big process.

Last edited by tuxdev; 05-08-2006 at 06:37 PM.
 
Old 05-08-2006, 07:40 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by tuxdev
For this thing, I like the idea of an array of pointers for each procedure.
Normally you would make that array of pointers a stack for each procedure, because the stack nicely mirrors the way procedures and functions are called.

The process would benefit from it's own pseudo heap if it was going to allow globals.
 
Old 05-08-2006, 09:55 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Yes - "exit()" will absolutely clean up everything you've malloc'ed.

And yes, although it's OK to rely on "exit()" for small, toy programs, it's arguably poor form. You should get into the habit of "cleaning up after yourself" so that you won't foget to do it when it counts (like writing a server daemon, for example ;-)).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
*** glibc detected *** malloc() / free()/ double RohanShrivastav Programming 12 10-01-2012 10:08 AM
how does malloc() and free() functions work? kuna Programming 8 12-04-2004 03:59 AM
malloc/free in C h/w Programming 12 02-26-2004 01:13 PM
malloc/free and segfault - advanced question iTux Programming 3 12-10-2003 04:51 PM
Is my malloc/free thinking correct? registering Programming 6 06-18-2003 11:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration