| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | free() |
While listening to Frank Adam Pete heard:free()
G'day Frank
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>
FA> char* foo()
FA> {
FA> char *s = malloc(80);
FA> }
char * foo(void);
int main(void) {
char *mymem;
mymem = foo();
free(mymem);
return 0;
}
char * foo(void) {
char *s = (char*) malloc(80);
/* do stuff to allocated mem here */
return s;
}
FA> If this function is to return s, freeing it before return would
FA> not be a good idea, is that true ?
should work just fine just don't forget to add the 'return' to the end
of your foo function.
FA> If it is, how would i then get access to s to free it ? It's out
FA> of scope, as silly as is i've tried free(s) from main, compiler
FA> says "what s?" of course.
have a pointer ready to accept the return pointer value.
FA> Btw, i have thought about freeing it before returning, and it
FA> *seems* to work fine in DOS, but i don't know how safe that is.
Unless there's a reason to return a pointer to the allocated mem then
free it before you return from the function.
FA> I also believe that in a multitask environment, that would be
FA> fatal if another program got a slice between free() and return,
FA> am i thinking straight here or not ?
not sure if i understand what you mean here?.
-=Pete=-
--- OMX/Blue Wave v2.12
* Origin: Gates of Hell (3:713/914.16)SEEN-BY: 50/99 620/243 623/630 711/401 409 410 413 430 808 809 932 934 SEEN-BY: 712/508 515 713/111 317 601 611 615 618 700 826 888 914 714/906 SEEN-BY: 800/1 @PATH: 713/914 615 888 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™.