473,395 Members | 1,516 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,395 software developers and data experts.

Need to volatile a by-reference parameter?

I've got some code that looks somewhat like this:

typedef struct TAG_STREAMDATA
{
// Lots of stuff in here ...
double param1, param2;
} STREAMDATA;

class Streamer
{
public:
void GetElement(int id);
int Count(void);
int SetStreamData(STREAMDATA *sd);
};

bool FilterFunc(STREAMDATA &sd) const
{
// Returns true or false depending on the contents of sd.
}

void SomeCalculation(const double p1, const double p2)
{
// Does some stuff
}

void CalculateAll(Streamer &AStreamer, double *p1, double *p2,
STREAMDATA &sd)
{
int NumElements = AStreamer.Count();
for (int id = 0; id < NumElements; id++)
{
AStreamer.GetElement(id);
if (FilterFunc(sd)) SomeCalculation(*p1, *p2);
}
}

void CalculateSpecific(Streamer &AStreamer)
{
STREAMDATA sd;
AStreamer.SetStreamData(&sd);
CalculateAll(AStreamer, &(sd.param1), &(sd.param2), sd);
}
Now, the question is, do I need to declare the sd parameter of CalculateAll
as volatile? The compiler cannot see from the code that sd is changed in the
loop, so my worry is that the call to FilterFunc will be optimised out of
the loop (due to it being a const function). It seems to me that it comes
down to how by-reference parameters are treated by the compiler. If they are
treated as syntactic sugar over pointers, then I think it would operate
correctly (as compilers do not optimise out function calls with pointer
parameters due to the impossibility of figuring out if something has
changed). However, if they are treated as a by-value non-pointer paramter,
then it would fail (the FilterFunc call, being const, could and should be
optimised out of the loop since sd never leaves the function).

Volatile would, as far as I can tell, fix this problem (as would the simpler
solution of just using a pointer instead of by-reference parameters), but I
would still like to know which way by-reference parameters are treated.

Useful references or pointers also accepted :)

--
Michael Brown
Add michael@ to emboss.co.nz ---+--- My inbox is always open
Sep 28 '05 #1
1 1778
do I need to declare the sd parameter of CalculateAll as volatile?
No - the compiler should take care of that ....

If they are treated as syntactic sugar over pointers.
Without thinking about their different C++ behavior there should be no
difference.
They simply refer to a memoryadress.

Sep 28 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andrew | last post by:
Section 17.4.3 of the ECMA-334 C# Language Specification says 1 When a field-declaration includes a volatile modifier, the fields introduced by that declaration are volatile fields. 2 For...
17
by: Radde | last post by:
HI, Can volatile variables be accessed by many processess..or only one process can access it.. Cheers..
9
by: Tim Rentsch | last post by:
I have a question about what ANSI C allows/requires in a particular context related to 'volatile'. Consider the following: volatile int x; int x_remainder_arg( int y ){ return x % y; }
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...
14
by: google-newsgroups | last post by:
Hello, even (or because?) reading the standard (ISO/IEC 9899/1999) I do not understand some issues with volatile. The background is embedded programming where data is exchanged between main...
17
by: dingoatemydonut | last post by:
The C99 standard states: "In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce...
9
by: mlimber | last post by:
I am using placement new to locate a shared data structure at a particular location in shared memory. The problem is that when I access the data on one of the two processors sharing it, I don't get...
8
by: CJ | last post by:
The following tries to make thread-safe code while avoiding the high cost of a lock. But is it kosher? // file or global scope level int X; volatile _Bool IsInitializedX=false;
10
by: S James S Stapleton | last post by:
Is volatile necessary anymore? I have a two-thread piece of code I've been testing to figure out what volatile does (fairly simple code, uses pthreads). I have an update thread (variables passed as...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.