By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,376 Members | 1,668 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,376 IT Pros & Developers. It's quick & easy.

`volatile' on local variable used outside of function

P: n/a
Let's say I have a situation like this:

/* begin example.c */
static int* ptr;

static void inner_fn(void)
{
*ptr = 1;
}

void outer_fn(void)
{
int i = 0;
ptr = &i;
inner_fn();
printf("%d\n", i);
}
/* end example.c */

in which a global pointer is set to point to a function-local variable
and then written to in another function. Do I then have to declare `i'
as `volatile' to get the expected behavior?

Dec 27 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <11**********************@g49g2000cwa.googlegroups .com>,
nickptar <ni******@gmail.com> wrote:
Let's say I have a situation like this: /* begin example.c */
static int* ptr; static void inner_fn(void)
{
*ptr = 1;
} void outer_fn(void)
{
int i = 0;
ptr = &i;
inner_fn();
printf("%d\n", i);
}
/* end example.c */ in which a global pointer is set to point to a function-local variable
and then written to in another function. Do I then have to declare `i'
as `volatile' to get the expected behavior?


No.

Effectively, you only need volatile for variables that might
be changed by something outside the normal flow of control,
such as by a signal handler.

You do, though, need to #include <stdio.h> to get the expected behaviour.
And you will need a main() somewhere along the line.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Dec 27 '05 #2

P: n/a
nickptar wrote:
/* begin example.c */
static int* ptr;

static void inner_fn(void)
{
*ptr = 1;
}

void outer_fn(void)
{
int i = 0;
ptr = &i;
inner_fn();
printf("%d\n", i);
}
/* end example.c */

Do I then have to declare `i'
as `volatile' to get the expected behavior?


Any decent compiler should detect that you're using a pointer to this
local variable. I don't think you need to declare it "volatile".

If I'm wrong, I'd like to see a reasonable explanation.
Dec 27 '05 #3

P: n/a
In article <11**********************@g49g2000cwa.googlegroups .com>,
"nickptar" <ni******@gmail.com> wrote:
Let's say I have a situation like this:

/* begin example.c */
static int* ptr;

static void inner_fn(void)
{
*ptr = 1;
}

void outer_fn(void)
{
int i = 0;
ptr = &i;
inner_fn();
printf("%d\n", i);
}
/* end example.c */

in which a global pointer is set to point to a function-local variable
and then written to in another function. Do I then have to declare `i'
as `volatile' to get the expected behavior?


If the "expected behavior" is that calling inner_fn() sets i to 1, then
there is absolutely no need to declare i as volatile. "volatile" is used
for objects that could be modified by things outside your C code.

Actually, declaring i as volatile int would be a huge mistake: If an
object is volatile, then it is illegal to modify it through a
non-volatile pointer, and it invokes undefined behavior. You would have
to change the declaration of ptr to:

static volatile int* ptr;
Dec 27 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.