`volatile' on local variable used outside of function

Discussion in 'C Programming' started by nickptar, Dec 27, 2005.

  1. nickptar

    nickptar Guest

    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?
    nickptar, Dec 27, 2005
    #1
    1. Advertising

  2. In article <>,
    nickptar <> 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
    Walter Roberson, Dec 27, 2005
    #2
    1. Advertising

  3. nickptar

    Guillaume Guest

    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.
    Guillaume, Dec 27, 2005
    #3
  4. In article <>,
    "nickptar" <> 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;
    Christian Bau, Dec 27, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    5
    Views:
    579
    Ulrich Eckhardt
    Jan 11, 2005
  2. FireHead

    Deallocating Local Variable used by Malloc

    FireHead, Oct 16, 2006, in forum: C Programming
    Replies:
    5
    Views:
    333
    Old Wolf
    Oct 17, 2006
  3. tiffini
    Replies:
    7
    Views:
    481
    Bluejack
    Mar 13, 2007
  4. sairam
    Replies:
    2
    Views:
    354
    Steve Holden
    Apr 5, 2007
  5. Krzysztof Poc

    outside type, outside function

    Krzysztof Poc, Feb 3, 2012, in forum: C++
    Replies:
    1
    Views:
    271
    Victor Bazarov
    Feb 7, 2012
Loading...

Share This Page