Re: `volatile' on local variable used outside of function



In article <1135704237.460269.312410@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
nickptar <nickptar@xxxxxxxxx> 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
.



Relevant Pages

  • [PATCH 2/2] ide-mpc8xx: fix resources reservation
    ... Use request_mem_regionfor resources reservation. ... static int identify; ... static void print_fixed (volatile u8 *p); ...
    (Linux-Kernel)
  • [git patches] new wireless stuffs
    ... patch with a Linksys WPC11v3. ... All this new wireless code adds near-infinite amounts of trailing whitespace. ... static void timer_func; ... static int takedown_proc_entry(struct net_device *dev, ...
    (Linux-Kernel)
  • [PATCH 5/7] Add mISDN HFC PCI driver
    ... new file mode 100644 ... +static int HFC_cnt; ...
    (Linux-Kernel)
  • Re: gjournal and zfs, questions
    ... It's the same as the other one available from the mailing lists ... this list of conditions and the following disclaimer in the ... +static void putcgs; ...
    (freebsd-questions)
  • [PATCH] 3/5: Device-mapper: snapshots
    ... tristate "Snapshot target " ... +struct disk_exception { ... +static int chunk_io ... +static void persistent_fraction_full(struct exception_store *store, ...
    (Linux-Kernel)