Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > 'static' : strange behavior

Reply
Thread Tools

'static' : strange behavior

 
 
Mark
Guest
Posts: n/a
 
      12-28-2010
I'm working with a code (not mine) on embedded MIPS platform and ran across
a weird behavior. I have a function:

int nvram_init(void *si)
{
static int nvram_status = -1;
...
}

This function gets called only at one place of the firmware, what really
strange is that output value of 'nvram_status' at the very beginning of the
'nvram_init()' is always 0.

As per my knowledge, a variable declared 'static' withing a function's body
must maintain it value between the function's calls, but it doesn't seem to
be the case here! Should I be looking for some hardware/compiler specific
trick, or I'm missing something more trivial?

--
Mark

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      12-28-2010
On 12/28/10 02:57 PM, Mark wrote:
> I'm working with a code (not mine) on embedded MIPS platform and ran
> across a weird behavior. I have a function:
>
> int nvram_init(void *si)
> {
> static int nvram_status = -1;
> ...
> }
>
> This function gets called only at one place of the firmware, what really
> strange is that output value of 'nvram_status' at the very beginning of
> the 'nvram_init()' is always 0.


Two questions, what do you mean by "at the very beginning" and how do
you know?

> As per my knowledge, a variable declared 'static' withing a function's
> body must maintain it value between the function's calls, but it doesn't
> seem to be the case here! Should I be looking for some hardware/compiler
> specific trick, or I'm missing something more trivial?


It should.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-28-2010
On 12/27/2010 8:57 PM, Mark wrote:
> I'm working with a code (not mine) on embedded MIPS platform and ran
> across a weird behavior. I have a function:
>
> int nvram_init(void *si)
> {
> static int nvram_status = -1;
> ...
> }
>
> This function gets called only at one place of the firmware, what really
> strange is that output value of 'nvram_status' at the very beginning of
> the 'nvram_init()' is always 0.


How do you know this? (See below.)

> As per my knowledge, a variable declared 'static' withing a function's
> body must maintain it value between the function's calls, but it doesn't
> seem to be the case here! Should I be looking for some hardware/compiler
> specific trick, or I'm missing something more trivial?


Yes, a `static' variable retains its last-stored[*] value from
one function call to the next, or more generally from one assignment
to the next. If there has been no assignment, the `static' variable
holds it initialization value, or "the right kind of zero" if there
is no initializer. Based on what you've shown, `nvram_status' should
have the value minus one the first time nvram_init() is called. I
therefore suspect that you have not shown everything that's relevant.
In particular, you haven't shown how you determine the original value
of `nvram_status' -- and it's possible that the determination itself
might be at fault.
[*] For a `volatile' variable, "last-stored" might not be a visible
action of the program: a hardware clock or some such might "store" a
new value in the variable without an explicit assignment by the program.
However, `nvram_status' is not `volatile', so we need not consider that
potential complication.


--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      12-28-2010
Eric Sosman wrote:
> Yes, a `static' variable retains its last-stored[*] value from
> one function call to the next, or more generally from one assignment
> to the next. If there has been no assignment, the `static' variable
> holds it initialization value, or "the right kind of zero" if there
> is no initializer. Based on what you've shown, `nvram_status' should
> have the value minus one the first time nvram_init() is called. I
> therefore suspect that you have not shown everything that's relevant.
> In particular, you haven't shown how you determine the original value
> of `nvram_status' -- and it's possible that the determination itself
> might be at fault.


I print the value of 'nvram_status', below is more complete snippet:

int nvram_init (void *si)
{
int ret;
static int nvram_status = -1;

printf("%s : nvram_status=%d\n", __FUNCTION__, nvram_status); /* my
line */

if (nvram_status == 1) /* the very first statement in the original code
*/
return 1;

/* low-level initializations of NVRAM */
/* nvram_status can be altered down here */
...
}

>[*] For a `volatile' variable, "last-stored" might not be a
> visible action of the program: a hardware clock or some such might
> "store" a new value in the variable without an explicit assignment by
> the program. However, `nvram_status' is not `volatile', so we need
> not consider that potential complication.



--
Mark

 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      12-28-2010
Ian Collins wrote:
>> This function gets called only at one place of the firmware, what
>> really strange is that output value of 'nvram_status' at the very
>> beginning of the 'nvram_init()' is always 0.

>
> Two questions, what do you mean by "at the very beginning" and how do
> you know?


See my reply to Eric Sosman.

--
Mark
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      12-28-2010
On 12/28/10 03:24 PM, Mark wrote:
> Eric Sosman wrote:
>> Yes, a `static' variable retains its last-stored[*] value from
>> one function call to the next, or more generally from one assignment
>> to the next. If there has been no assignment, the `static' variable
>> holds it initialization value, or "the right kind of zero" if there
>> is no initializer. Based on what you've shown, `nvram_status' should
>> have the value minus one the first time nvram_init() is called. I
>> therefore suspect that you have not shown everything that's relevant.
>> In particular, you haven't shown how you determine the original value
>> of `nvram_status' -- and it's possible that the determination itself
>> might be at fault.

>
> I print the value of 'nvram_status', below is more complete snippet:
>
> int nvram_init (void *si)
> {
> int ret;
> static int nvram_status = -1;
>
> printf("%s : nvram_status=%d\n", __FUNCTION__, nvram_status); /* my line */
>
> if (nvram_status == 1) /* the very first statement in the original code */
> return 1;


Why do you set the value to -1 and test for 1?

--
Ian Collins
 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      12-28-2010
Ian Collins wrote:
>> int nvram_init (void *si)
>> {
>> int ret;
>> static int nvram_status = -1;
>>
>> printf("%s : nvram_status=%d\n", __FUNCTION__, nvram_status); /* my
>> line */
>>
>> if (nvram_status == 1) /* the very first statement in the original
>> code */ return 1;

>
> Why do you set the value to -1 and test for 1?


I don't; as I mentioned earlier this is not my code.

--
Mark
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      12-28-2010
On 12/27/2010 9:24 PM, Mark wrote:
> Eric Sosman wrote:
>> Yes, a `static' variable retains its last-stored[*] value from
>> one function call to the next, or more generally from one assignment
>> to the next. If there has been no assignment, the `static' variable
>> holds it initialization value, or "the right kind of zero" if there
>> is no initializer. Based on what you've shown, `nvram_status' should
>> have the value minus one the first time nvram_init() is called. I
>> therefore suspect that you have not shown everything that's relevant.
>> In particular, you haven't shown how you determine the original value
>> of `nvram_status' -- and it's possible that the determination itself
>> might be at fault.

>
> I print the value of 'nvram_status', below is more complete snippet:
>
> int nvram_init (void *si)
> {
> int ret;
> static int nvram_status = -1;
>
> printf("%s : nvram_status=%d\n", __FUNCTION__, nvram_status); /* my line */
>
> if (nvram_status == 1) /* the very first statement in the original code */
> return 1;
>
> /* low-level initializations of NVRAM */
> /* nvram_status can be altered down here */
> ...
> }


It seems you like to dispense information drop by precious drop,
one tiny tidbit at a time. If the information is so very private that
you dare not share it except in meaningless driblets -- well, I have
never been known as a patient man, and I think I've got better things
to do than spend time wheedling details from a coy Twenty Questions
player.

If and when you want help from me, provide sufficient information
to get my analysis started. Until then, I've got more pressing matters
to attend to -- it's been *ages* since I last plucked my eyebrows.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Shivanand Kadwadkar
Guest
Posts: n/a
 
      12-28-2010
On Dec 28, 7:24*am, "Mark" <(E-Mail Removed)> wrote:
> Eric Sosman wrote:
> > * * Yes, a `static' variable retains its last-stored[*] value from
> > one function call to the next, or more generally from one assignment
> > to the next. *If there has been no assignment, the `static' variable
> > holds it initialization value, or "the right kind of zero" if there
> > is no initializer. *Based on what you've shown, `nvram_status' should
> > have the value minus one the first time nvram_init() is called. *I
> > therefore suspect that you have not shown everything that's relevant.
> > In particular, you haven't shown how you determine the original value
> > of `nvram_status' -- and it's possible that the determination itself
> > might be at fault.

>
> I print the value of 'nvram_status', below is more complete snippet:
>
> int nvram_init (void *si)
> {
> * * int ret;
> * * static int nvram_status = -1;
>
> * * printf("%s : nvram_status=%d\n", __FUNCTION__, nvram_status); */* my
> line */
>
> * * if (nvram_status == 1) */* the very first statement in the original code
> */
> * * * * return 1;
>
> * * /* low-level initializations of NVRAM */
> * * /* nvram_status can be altered down here */
> * * ...
>
> }
> > * *[*] For a `volatile' variable, "last-stored" might not be a
> > visible action of the program: a hardware clock or some such might
> > "store" a new value in the variable without an explicit assignment by
> > the program. However, `nvram_status' is not `volatile', so we need
> > not consider that potential complication.

>
> --
> Mark


I tried the same program i got -1 as output for all try. i have seen
no weird behavior.

nvram_init : nvram_status=-1
nvram_init : nvram_status=-1
nvram_init : nvram_status=-1
nvram_init : nvram_status=-1
nvram_init : nvram_status=-1
nvram_init : nvram_status=-1
nvram_init : nvram_status=-1
nvram_init : nvram_status=-1
nvram_init : nvram_status=-1
nvram_init : nvram_status=-1

Mark you can give some more information like compiler and may be
optimization that you are trying So that some people can better
analyze the problem
 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      12-28-2010
Eric Sosman wrote:
> It seems you like to dispense information drop by precious drop,
> one tiny tidbit at a time. If the information is so very private that
> you dare not share it except in meaningless driblets -- well, I have
> never been known as a patient man, and I think I've got better things
> to do than spend time wheedling details from a coy Twenty Questions
> player.



Appreciate your delightful sense of humour, it indeed cheered me up. The
information isn't private at all, at first I thought the function is pretty
lengthy to be posted here, therefore I provided just a snippet as you asked.
I've posted the source of the routine under question at
http://clc.pastebin.com/f4FRqgit

--
Mark

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thunderbird strange behavior... Jim Firefox 5 11-17-2005 03:09 PM
Firefox 1.04 and Strange Find Behavior Thomas Firefox 5 06-28-2005 08:40 PM
utf8 pragma - strange behavior ryang Perl 1 04-11-2005 05:38 AM
strange behavior when using 'read' sstark Perl 0 03-06-2005 02:27 AM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments