Pieter Claassen said:
I have a number of functions that does the following:
char * test;
....
return test;
}
I am aware that this will allocated a memory for a pointer on the stack
and then return that.
You don't allocate anything here, you just got an automatic variable
(that may be on the stack or somewhere else, you can't tell unless
you find out how your implementation is doing things, but which would
make it off-topic here). If you return from the function it will
vanish automatically.
And, no you're not returning the pointer, you're returning what it's
pointing to. Big difference! And since you don't show anything that
sets the value of 'test' it's impossible to say if you return anything
at all that's not some random, unitialized value.
1. How do I free that memory?
There's no memory allocated here (at least not that you show it), so
you can't free() anything. The 'test' pointer itself will vanish into
thin air when you leave the function without you needing to do anything.
2. Is this healthy or should I be mallocing that memory? Even if it is for
a pointer.
All this makes no sense at all. You just have an automatic variable
that's pointing to nothing you can use. It's definitely unhealthy if
you return what 'test' points to unless you make it point to some
memory you own, e.g. memory you got by calling malloc() or calloc()
or realloc().
Regards, Jens