Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > `volatile' on local variable used outside of function

Reply
Thread Tools

`volatile' on local variable used outside of function

 
 
nickptar
Guest
Posts: n/a
 
      12-27-2005
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?

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      12-27-2005
In article <(E-Mail Removed) .com>,
nickptar <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Guillaume
Guest
Posts: n/a
 
      12-27-2005
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.
 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      12-27-2005
In article <(E-Mail Removed) .com>,
"nickptar" <(E-Mail Removed)> 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;
 
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
outside type, outside function Krzysztof Poc C++ 1 02-07-2012 04:06 PM
help with pix inside->outside + dmz->outside + inside->outside->dmz Jack Cisco 0 09-19-2007 01:57 AM
Can we make a local variable in a function as global variable??? sairam Python 2 04-05-2007 07:16 PM
Deallocating Local Variable used by Malloc FireHead C Programming 5 10-17-2006 12:47 AM
a static local variable in a static method is thread local storage? Patrick Hoffmann C++ 3 08-08-2003 02:37 PM



Advertisments