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> }
FA> If this function is to return s, freeing it before return would not
FA> be a good idea, is that true ?
That is correct.
FA> If it is, how would i then get access to s to free it ? It's out of
How do you get access to s to USE it in the first place? Surely
you are going something like this:
t = foo();
???
If so, then to free the memory, just go free(t);
FA> scope, as silly as is i've tried free(s) from main, compiler says
FA> "what s?" of course.
FA> Btw, i have thought about freeing it before returning, and it *seems* to
FA> work fine in DOS, but i don't know how safe that is.
This is quite confusing, but I think what you mean is you free it
and you CONTINUE to use it. That is as unsafe as jumping off the
Sydney Harbour Bridge using a hanky as a parachute. You end up
getting wet.
FA> I also believe that in a multitask environment, that would be fatal if
FA> another program got a slice between free() and return, am i thinking
FA> straight here or not ?
Sort of and sort of not. BFN. Paul.
@EOT:
---
* Origin: X (3:711/934.9)
|