Hello,
in main function, I declare a pointer called MY_VAR. It points to type called TYPE, it is a structure. In function called from main function there is set a variable of type TYPE and the function returns the address of this variable. But if my program continues, another call of some function destroys the data stored on address where MY_VAR pointer points. How to tell the compiler not to destroy the data in *MY_VAR?
void main(void) { TYPE *MY_VAR; MY_VAR = setFunction(); anotherFunction(); // it destroys data in *MY_VAR}TYPE *setFunction() { TYPE result; // result = ...... return &result;}
Solved! Go to Solution.
You can just declare your own "heap" space with a static variable, like:
static int heap[25];
That will give you space, integer aligned, that is 100 bytes in size (25*4), and the compiler won't put anything else there. You can then keep track of who is using what portions of that space.
Alternately, you can allocate the space in the linker command file, but that's probably more work.
Unless I completely misunderstand you, setFunction() is returning a pointer to a variable on its stack, which is implicitly deallocated (and available for reuse) by the time setFunction() returns.
I'd suggest that you either make "result" static in setFunction(), or allocate it on the stack in main() and pass its address to setFunction(), rather than vice versa.
Does that make sense?
-- Rich
I'm begginner, so sorry for that stupid question. Static keyword is what helps me, partially...
Is there any way to deallocate this static variables. My program gets to the stage where the static variable is not needed. I guess it is not possible, so is there any way to dynamically allocate variables?
As Rich already suggested, you can pass the address of a local into setFunction instead of retuning it.
Depending on what TYPE is, the "setFunction" (quotes as it is more a get-function actually) could also return TYPE, and not TYPE*.
I would not go with static for various reasons, one is, as you noted, statics cannot be deallocated. Another reason is that there is only a single static instance ever, whereas with locals different function invocations have their own copy.
Using free/malloc is technically possible, but I would not do it on a small chip. Leaking memory can cause problematic bugs and it also makes the code harder to get correct as the caller now must remember to call free. Also dynamically allocated memory has to managed, that causes additional overhead.
Daniel
void main(void) { TYPE MY_VAR; setFunction(&MY_VAR); anotherFunction(); } void setFunction(TYPE* result) { // ...... *result = ...... return; }
You can just declare your own "heap" space with a static variable, like:
static int heap[25];
That will give you space, integer aligned, that is 100 bytes in size (25*4), and the compiler won't put anything else there. You can then keep track of who is using what portions of that space.
Alternately, you can allocate the space in the linker command file, but that's probably more work.
You can't deallocate a static variables... Local variables are dynamically allocated on the stack and are implicitly deallocated when the function returns.
You may be able to use malloc() and free() to allocate heap variables dynamically and free them when you no longer need them, depending on which libraries you are linking to and the heap settings in your linker command file (lcf).
See if there is a document like "MSL C Reference" or maybe a Compiler manual in your Help/PDF directory from CodeWarrior -- I'm not sure what version of CW you are using, or for what MCU.
-- Rich
Here's a good explanation of the problem:
http://en.wikipedia.org/wiki/Wild_pointer
Search for "returning addresses of a stack-allocated local variable".