J
jaysome
Suppose I have something like this on a free-standing
environment (where void main(void) is documented and well-defined):
static int G_num_loops = 0;
void main (void)
{
/* set up interrupts */
/* ... */
for ( ; ; )
{
G_num_loops++;
}
}
Further suppose that I have the (non-standard) means to "read" the value
of G_num_loops via a link map and other techniques
Is there anything in the C standard that allows the compiler to
optimize away and NOT allocate storage for G_num_loops and thus prevent me
from "viewing" its value? If so, does defining G_num_loops as volatile
prevent this? (or put in a more standard kind of way, does the C standard
require the statement:
G_num_loops++;
to be executed.
My compiler seems to be optimizing that statement away when defined as
static and not volatile. So instead, I change the definition to have
external linkage:
int G_num_loops = 0;
and it works for my purpose.
The only *potential* problem is that if I define variables with the
same name (mostly arrays) with external linkage in another translation
unit (more likely the name would be G_debug_buf), then the linker issues
an error about multiply defined symbols.
I can deal with that--I've learned to "work" with the compiler and
linker and just name such variables with prefixes that are specific to the
module, e.g., G_fir_debug_buf or G_serial_debug_buf. Maybe I'm just facing
the reality of embedded debugging, but I'd like to hear the opinions of
others who have similar experience in this area.
BTW, the compiler I'm working with does not conform to the C standard in
that it does not initialize the following file scope definition to 0:
static int G_num_loops;
I have to explicitly initialize it to 0:
static int G_num_loops = 0;
Thanks
environment (where void main(void) is documented and well-defined):
static int G_num_loops = 0;
void main (void)
{
/* set up interrupts */
/* ... */
for ( ; ; )
{
G_num_loops++;
}
}
Further suppose that I have the (non-standard) means to "read" the value
of G_num_loops via a link map and other techniques
Is there anything in the C standard that allows the compiler to
optimize away and NOT allocate storage for G_num_loops and thus prevent me
from "viewing" its value? If so, does defining G_num_loops as volatile
prevent this? (or put in a more standard kind of way, does the C standard
require the statement:
G_num_loops++;
to be executed.
My compiler seems to be optimizing that statement away when defined as
static and not volatile. So instead, I change the definition to have
external linkage:
int G_num_loops = 0;
and it works for my purpose.
The only *potential* problem is that if I define variables with the
same name (mostly arrays) with external linkage in another translation
unit (more likely the name would be G_debug_buf), then the linker issues
an error about multiply defined symbols.
I can deal with that--I've learned to "work" with the compiler and
linker and just name such variables with prefixes that are specific to the
module, e.g., G_fir_debug_buf or G_serial_debug_buf. Maybe I'm just facing
the reality of embedded debugging, but I'd like to hear the opinions of
others who have similar experience in this area.
BTW, the compiler I'm working with does not conform to the C standard in
that it does not initialize the following file scope definition to 0:
static int G_num_loops;
I have to explicitly initialize it to 0:
static int G_num_loops = 0;
Thanks