Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

static variable in method !

Status
Not open for further replies.

karephul

Programmer
May 14, 2006
24
US

I am trying to make an application for MOD1253 coldfire (microprocessor)using netburner dev c++ 1.2 IDE for my development. I need to make the application without using RTOS below is the code which is giving me weired error.

Code:
void method(){
    My_writechar(0,c);
    static int pinn = 4;
    int pinn1 = 4;    
    char ch=(char)(pinn+'0'); // convert to char
    char ch1=(char)(pinn1+'0');
    //method which prints char, without using standar IO
    My_writechar(0,ch);   // prints l
    My_writechar(0,ch1);  // prints 4
}

this method works fine with RTOS, but doest not work without it.

it should print 4 .. but it prints something weired. I tested the same code with Visual Studio, and it works fine if I use std::cout<<ch;

I will really appreciate your quick response.

Karephul
 
> static int pinn = 4;
Something has to take responsibility for initialising this variable as the program starts. If you rip out enough things, then eventually it will become your responsibility.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Static variables should initialize with the value given on first invocation but thereafter, it remembers the last value it had. The problem with embedded stuff is that this initialization is not always guaranteed. Have a look at it in assembler. If it is something like
Code:
pinn DB 4
it is not going to work because when you powerup, preset values do not keep. The only thing you can rely on is that the last value will be remembered on re-entry.
 
Code:
(pinn+'0'); // convert to char
This is not a portable way to convert a number into a character. It requires the code to only be run on ASCII systems.
sprintf() would be a better choice.
 

wvb and salem,

I was expecting the same problem !!, What is the best way to make sure I will be able to initialize that static variable ? I am not sure which way I should proceed.

so, wvb what would you suggest at this time, if I want this functionality in my present code ? any other way arounds ? BTW do you know how is this local static variable store in memory ?? this might give me some direction ??
 
Well in the absence of an RTOS, what calls method() ?

How does the program get onto the embedded device?

Is there some bootstrap code which runs before calling method() ?
Does that bootstrap code also set up a stack, to enact the call, and provide somewhere to store local variables?

In some systems, when you've passed the object file through the linker, then you'll have a .text section containing the program code, and a .data section containing initialised data (your static for example). This .data needs to be stored in ROM, then copied to RAM at program start.

Lots of reading of your target tools manuals is in order.

> It requires the code to only be run on ASCII systems.
The 'C' standard requires that '0' through '9' are contiguous, so this code should be fine.
c99 said:
In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Here are a few ways around this, none of them elegant.

1) Make it a global variable and initialize it in some initialization routine.
2) Add a parameter to method. If set, it initializes the variable, otherwise it continues
3) If the variable never changes, it is a const so you could make it const in which case it will go into ROM instead of in RAM.

You wouldn't believe how difficult it was as a junior programmer to convince senior mainframe programmers that their static variables do not initialize correctly in embedded systems.
 
[youwrote ]In some systems, when you've passed the object file through the linker, then you'll have a .text section containing the program code, and a .data section containing initialised data (your static for example).
[/youwrote]

I am trying to see where local static are stored in the map file(Linker address map) but it is not there ? So, It might be in the stack (though it doest make much sence). But I cannot think of something else if its not in other sections.

I have main() and from there I am calling method(). I think main() is handeled by

Code:
C:\\\\Nburn\\\\lib\\\\MOD5213.a(atexit.o)
                              C:/Nburn/gcc-m68k/bin/../lib/gcc/m68k-elf/3.4.2/../../../..
/m68k-elf/lib/m5206e/crt0.o (atexit)
C:\\\\Nburn\\\\lib\\\\NetBurner_nn.a(librarystubs.o)
                              C:/Nburn/gcc-m68k/bin/../lib/gcc/m68k-elf/3.4.2/../../../..
/m68k-elf/lib/m5206e/crt0.o (exit)

Let me know if you have any comments on what I think, and how can I solve this probelm ?? Is there any other way I can have the same functionality without using local static variables. ??
 
It won't be on the stack. It will be somewhere in .data.

Try this
Code:
int glob;
main ()
{
   glob = 0x5555AAAA;
   return 0;
}
And this
Code:
int glob;
main ()
{
   static int locglob;
   locglob = 0x5555AAAA;
   return 0;
}
See if you can locate the variable by looking for 5555AAAA in the generated code.
 
Have you located the linker script yet?
Most(?) embedded systems need some kind of script to describe the physical layout of memory so that the linker can put the code and data in the right places.

> I am trying to see where local static are stored in the map file(Linker address map) but it is not there ?
Map files typically contain globals only.

What you might find however are special symbols like
_stext _etext _sdata and _edata
These mark the extents of code and data for example.

> Is there any other way I can have the same functionality without using local static variables. ??
Make it global, and initialise it at the start of main.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top