TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Frank Adam
from: rowan_crowe
date: 1996-05-24 03:54:36
subject: free()

* Frank Adam writes to All, on Wednesday May 22 1996
   at 20:17:

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

 FA> eg:
 FA> main()
 FA> {
 FA>  foo();
 FA>  }

 FA> char* foo()
 FA> {
 FA>  char *s = malloc(80);
 FA>  }

Why not turn it around, instead the CALLING procedure does the malloc and
provides a pointer to the allocated memory. Your function then makes use of
that memory. The calling procedure is responsible for the cleanup.

Another way is to return a pointer to the allocated memory, again the
caller is responsible for freeing it.

 FA> If this function is to return s, freeing it before return would not
 FA> be a good idea, is that true ?

Correct.

 FA> Btw, i have thought about freeing it before returning, and it *seems*
 FA> to work fine in DOS, but i don't know how safe that is. I also believe
 FA> that in a multitask environment, that would be fatal if another
 FA> program got a slice between free() and return, am i thinking straight
 FA> here or not ?

If it works on one platform, no guarantees it will not break (sometimes in
horrible ways) on another. Using memory that is not allocated to you is
asking for trouble.

 FA>    L8r Frank(fadam{at}ozemail.com.au)

You need an ISP than can provide:

    a) Cheap access
    b) Reliable and fast data transfer

Hate to tell you this, but ozemail doesn't do either of those. 8-) I was a
customer of theirs for a short period, then took my custom elsewhere. If
you want some suggestions for other ISPs netmail me . . .

Cheers.

---
* Origin: PLEASE NOTE NEW ADDRESS ---> (3:635/728.1)
SEEN-BY: 50/99 620/243 623/630 632/349 635/503 544 727 728 637/106 711/401
SEEN-BY: 711/409 410 413 430 808 809 932 934 712/515 713/888 714/906 800/1
@PATH: 635/728 727 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™.