473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

`volatile' on local variable used outside of function

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
3 5186
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Radde | last post by:
HI, Can volatile variables be accessed by many processess..or only one process can access it.. Cheers..
1
by: Vijay Kumar R Zanvar | last post by:
Hello, I have a question: when do we use volatile qualified function parameters? Vijay
8
by: Tim Rentsch | last post by:
Here's another question related to 'volatile'. Consider the following: int x; void foo(){ int y; y = (volatile int) x;
8
by: Jason Curl | last post by:
I was reading a previous article about when to use volatiles. My program has a shared memory buffer that is being accessed by two threads. The threads are protected by an operating system...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
6
by: Clausfor | last post by:
Hello, I have a problem with restoring variables in the setjmp/longjmp functions: K&R2 for longjmp says: "Accessible objects have the same value they had when longjmp was called, except for...
6
by: bwaichu | last post by:
Can someone help me out here? This is one of those areas of C that I am fuzzy on. I'm not quite sure when I should use volatile. If you could provide an example, that would help out a lot. I...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.