Overwrited variable

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Overwrited variable

跳至解决方案
4,009 次查看
gaminn
Contributor IV

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;}
标签 (1)
标记 (1)
0 项奖励
回复
1 解答
2,074 次查看
RichTestardi
Senior Contributor II

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.

 

 

在原帖中查看解决方案

0 项奖励
回复
8 回复数
2,074 次查看
RichTestardi
Senior Contributor II

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

0 项奖励
回复
2,074 次查看
gaminn
Contributor IV

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?

0 项奖励
回复
2,074 次查看
CompilerGuru
NXP Employee
NXP Employee

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;
}

 


 

0 项奖励
回复
2,074 次查看
gaminn
Contributor IV
OK, you said that there are problems with using standard malloc and free functions. So I would like to program my own malloc and free functions, little memory manager... Only I need is to tell compiler not to store data to memory area (tens of bytes) where I intend to store my dynamic variables. How to do it?
0 项奖励
回复
2,075 次查看
RichTestardi
Senior Contributor II

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.

 

 

0 项奖励
回复
2,074 次查看
gaminn
Contributor IV
Yes, that was stupid question, I realized that it is simple as you said a minute after I switched my computer off. :smileyhappy: So thank you everybody, I think I know what I wanted to know.
0 项奖励
回复
2,074 次查看
RichTestardi
Senior Contributor II

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

0 项奖励
回复
2,074 次查看
RichTestardi
Senior Contributor II

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".

0 项奖励
回复