TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: All
from: Frank Adam
date: 1996-05-22 20:17:08
subject: free()

Hi,

Could someone tell or show me how to free dynamically allocated 
memory in one function from another function or main ?

eg:
main()
{
 foo();
 }
 
char* foo()
{
 char *s = malloc(80);
 }

If this function is to return s, freeing it before return would not 
be a good idea, is that true ?
If it is, how would i then get access to s to free it ? It's out of 
scope, as silly as is i've tried free(s) from main, compiler says 
"what s?" of course.
Btw, i have thought about freeing it before returning, and it *seems* to 
work fine in DOS, but i don't know how safe that is. 
I also believe that in a multitask environment, that would be fatal if 
another program got a slice between free() and return, am i thinking 
straight here or not ?                     

Thanks, 

   L8r Frank(fadam{at}ozemail.com.au)
   
___ Blue Wave/DOS v2.21

--- Maximus 3.01
* Origin: The Software Parlour (3:635/544)
SEEN-BY: 50/99 620/243 623/630 632/349 635/503 544 727 711/401 409 410 413
SEEN-BY: 711/430 808 809 932 934 712/515 713/888 714/906 800/1
@PATH: 635/544 50/99 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.