Search Members Help

» Welcome Guest
[ Log In :: Register ]

Mini-ITX Boards Sale, Fanless BareBones Mini-ITX, Bootable 1G DSL USBs, 533MHz Fanless PC <-- SALE $200 each!
Get The Official Damn Small Linux Book. DSL Market , Great VPS hosting provided by Tektonic
 

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Topic: c programming question, malloc/free functions< Next Oldest | Next Newest >
Lii Offline





Group: Members
Posts: 7
Joined: Nov. 2004
Posted: Feb. 20 2005,07:26 QUOTE

can someone clear something for me, please?
let's say I have the following code:

1:  int main(){
2:  SomeFunction();
3:  return 0;}
4:
5:  int SomeFunction(){
6:  char *ptr;
7:  ptr=(char *)malloc(sizeof(char)*20);
8:  //...do something with ptr
9:  free(ptr);
10: return 0;}

my question is: what happens if I don't use the free(ptr) on line 9? When is the memory deallocated?

-When I return from the SomeFunction function on line 10?
-When I exit the program on line 3?
-Never?
Back to top
Profile PM 
clacker Offline





Group: Members
Posts: 570
Joined: June 2004
Posted: Feb. 20 2005,23:35 QUOTE

Lii, when you leave out the free memory isn't freed, ever.  This is known as a "memory leak."

When you exit the function memory can't be freed by default, what if another function needed that memory and you put the pointer into a global variable?
Back to top
Profile PM 
Lii Offline





Group: Members
Posts: 7
Joined: Nov. 2004
Posted: Feb. 21 2005,06:18 QUOTE

Quote (clacker @ Feb. 20 2005,18:35)
Lii, when you leave out the free memory isn't freed, ever.  This is known as a "memory leak."

When you exit the function memory can't be freed by default, what if another function needed that memory and you put the pointer into a global variable?

that's really sad!???

thx for your time.
Back to top
Profile PM 
clacker Offline





Group: Members
Posts: 570
Joined: June 2004
Posted: Feb. 21 2005,11:58 QUOTE

not that sad, I mean it causes crashes and all, but memory leaks are all gone when you restart the machine, so it isn't really "never" it's until restart.
Back to top
Profile PM 
3 replies since Feb. 20 2005,07:26 < Next Oldest | Next Newest >

[ Track this topic :: Email this topic :: Print this topic ]

 
reply to topic new topic new poll
Quick Reply: c programming question

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code