LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 03-03-2004, 04:56 AM   #1
Hammo
Member
 
Registered: Feb 2003
Posts: 31

Rep: Reputation: 1
not 'free'ing 'malloc'ed memory


i have a question concerning malloc() and free().

i use malloc to allocate some memory in main() then pass the returned pointer to a thread. when an error occures in another thread in the same program i want to terminate the whole program by calling exit() in the error thread to terminate the process immediately.

my question is .... does the unfreed memory allocated in main get deallocated automatically when the process ends or will it remain reserved?

thanks.

Last edited by Hammo; 03-03-2004 at 04:57 AM.
 
Old 03-03-2004, 05:01 AM   #2
cjp
Member
 
Registered: Dec 2003
Location: the Netherlands
Distribution: SuSE 9.0
Posts: 54

Rep: Reputation: 15
Memory is a resource that is shared between the threads. I think that it will only be deallocated when all threads have terminated.

You can check this by making a test program that allocates a large memory area, so that you can see in e.g. top what happens
 
Old 03-03-2004, 05:04 AM   #3
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
Re: not 'free'ing 'malloc'ed memory

Quote:
Originally posted by Hammo
i have a question concerning malloc() and free().

i use malloc to allocate some memory in main() then pass the returned pointer to a thread. when an error occures in another thread in the same program i want to terminate the whole program by calling exit() in the error thread to terminate the process immediately.

my question is .... does the unfreed memory allocated in main get deallocated automatically when the process ends or will it remain reserved?

thanks.
If you are referring to process as a thread, then it should not be freed when the thread exits. If you are referring to process as the in-memory version of your program ( meaning, all threads ), then yes it does free at the end of execution -- as all mappings are removed.
 
Old 03-03-2004, 05:09 AM   #4
Hammo
Member
 
Registered: Feb 2003
Posts: 31

Original Poster
Rep: Reputation: 1
with process i mean the process which encapsulates all threads in the program
 
Old 03-03-2004, 05:30 AM   #5
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
Hey if a process gets terminated in Linux All the resources that are acquired by that process are reclaimed by system. I thihnk in linux automatic resource reclaimation is there. it applies to file descriptors, semaphores (check for this) etc.
I have a doubt regarding semaphores please check it.
 
Old 03-03-2004, 05:32 AM   #6
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
Mr. cjcuk semaphores are also taken back by the system right?
 
Old 03-03-2004, 07:08 AM   #7
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
Quote:
Originally posted by eshwar_ind
Mr. cjcuk semaphores are also taken back by the system right?
A call is invoked that removes the process from the semaphore queue.
 
Old 03-03-2004, 07:15 AM   #8
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
yes if its waiting for the semaphore. but i am asking about a process takes a semaphore and is killed before it returns that semaphore. Semaphore value is upadted by the system right?
 
Old 03-03-2004, 07:27 AM   #9
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
Quote:
Originally posted by eshwar_ind
yes if its waiting for the semaphore. but i am asking about a process takes a semaphore and is killed before it returns that semaphore. Semaphore value is upadted by the system right?
It has to, otherwise deadlock would occur any time that a process exited without releasing the semaphore.
 
Old 03-03-2004, 07:33 AM   #10
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
yes thank you very much
 
  


Reply



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
tool to detect number of memory slots, memory module type, speed, and size stefanwolf Linux - Hardware 8 04-22-2013 09:48 AM
Missing memory in USB stick, flash memory, removable hd etc bamboo_spider Linux - Newbie 3 06-14-2006 05:39 PM
malloc'ed members Jo_Nak Programming 3 07-23-2003 03:06 PM
Help!?! RH 8 Memory Mapping -High Memory-Virtural Memory issues.. Merlin53 Linux - Hardware 2 06-18-2003 04:48 PM
RH 8.0 Mapping Virtual Memory to get access to VMIC Reflective Memory PCI card. Merlin53 Linux - Hardware 0 05-05-2003 12:50 PM

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

All times are GMT -5. The time now is 06:24 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