std::atomic address after store - c++

I can't seem to get the address of an atomic object after a store.
e.g.
std::atomic<int> i;
std::atomic<int>* p = &++i; // doesn't work
auto* p = &++i; // doesn't work
// below works:
++i;
auto* p = &i;
What's happening here and why?
To clarify: I know it returns an r-value. Why doesn't it return the original object, this? Is this a purposeful design choice or was it an oversight?
More specifically, what's happening under-the-hood for this requirement?

While the pre-increment operator usually returns its operand by reference, in the case of std::atomic integers, it returns the new value as a temporary. So in your example ++i does not return a reference to the atomic<int> i itself, it returns the new value of i (i.e. an int). You can see this at: https://en.cppreference.com/w/cpp/atomic/atomic/operator_arith
It would be misleading and even dangerous to return a reference to the original atomic<int>, because to access the int value through this reference would require a second, separate read operation — so its value might be different from the value at the time of increment. (This isn't particularly relevant your example code, since you are only trying to obtain a pointer to the referenced object, but some code will actually access the value after ++ so this is why returning a reference isn't possible.)
In other words, if ++i returned a reference to the atomic<int> i, then
int j = ++i;
would be equivalent to
++i;
// ...other threads may modify the value of `i` here!...
int j = i;
The whole point of atomics is to perform reads and writes together as an indivisible operation, so ++i must internally use hardware/OS atomic operations to simultaneously read and increment the integer, so the new value is returned as a temporary.
If you're curious to see what's under the hood, here is libc++'s implementation where you can see that operator++ simply calls into fetch_add(1) and returns the result + 1.

Related

In C or C++, is there a way to get the address of i++ or ++i ("i" is int type)?

Is there a way to write something like below:
int i = 0;
int *p1 = &(i++);
int *p2 = &(++i);
Before asking "Is there a way", I think we have to ask, "What would it mean?"
In C, we have the concept of "object" versus "value". Basically, an object is a thing that can hold a value. So a variable like
int a;
is clearly an object, because it can hold a value — that is, we can say things like
a = 5;
to store a value into a.
The key distinction between an object and a value is that an object has a location. A value, on the other hand, is something we've computed that's just kind of floating in space, and if we don't find an object to store it in pretty soon, it disappears.
(Side note: You will sometimes come across the terms "lvalue" and "rvalue", which mean the same thing as "object" and "value" as I've been using them here. An lvalue is something that can appear on the left side of an assignment operator, while an rvalue is something that can only appear on the right.)
I've gone through this longish background introduction just so I can make this important point: You can only apply the address-of operator to an object, because only objects have locations. It makes perfect sense to say
int *p = &a;
But it would make no sense to say
int *p2 = &5; /* WRONG */
or
int *p3 = &(1 + 2); /* WRONG */
So it would equally make no sense to write
int *p4 = &(a + 1); /* WRONG */
The variable a is an object, but the thing that you get by fetching a's value and adding 1 to it is clearly just a value. By the time we've computed that value, it doesn't really matter (we might as well have forgotten) that one part of the computation of that value involved fetching a value from the object a.
But then we get you your questions. Suppose you try to write
int *p5 = &(i++); /* questionable */
int *p6 = &(++i);
The ++ operator takes a value and adds 1 to it, and the value i + 1 is clearly just a value, without a location. But, its true, i++ and ++i mean more than just "i + 1" — they compute the value i + 1 and store it back into i. So you can almost convince yourself that i++ and ++i have locations — but if they did, it would just be the location of the variable i. So you might as well have just said
int *p7 = &i;
Now, you might ask, "But what if I want to take the address of i, at the same time I increment it?" And the answer is, you're just going to have to do it in two steps:
int *p7 = &i;
i++;
Yes, it's true, the i++ and ++i operators are nice in that they let you do two things at once, things like
int x = array[i++];
or
int x = *p++;
But those are useful operations, because they come up all the time when you're working with arrays. But the need to say something like
int *p5 = &(i++);
comes up much less often, I think, so it's not nearly so important to have it work. (Me, I've been programming in C for 40 years, and I don't think I've ever felt the need to grab a pointer to i and increment it at the same time.) The ++ operator is something that often comes up in a loop, as i moves through an array or something. But since the address of i doesn't change, if you need a pointer to it, It makes sense to do that just once, before the loop (or whatever) even starts.
Finally, whether you agree with my explanations so far or not, the C Standard explicitly says that the result of the ++ and -- operators is an rvalue, not an lvalue. Two compilers I just tried this on gave me the errors
error: cannot take the address of an rvalue of type 'int'
and
error: lvalue required as unary ‘&’ operand
and these error messages pretty much say the same story I've been telling.
Although, to throw a pretty big monkey wrench into this story I've been telling, the rules are evidently different in C++! You can't do &(i++), but you can do &(++i)! I think there's a good reason for this, but I don't remember what it is.
I already know the question with this answer and apologize for the initial unclear question.
In C, we cannot use &(i++) and &(++i) and I think the reason is i++ and ++i are rvalues in C. However in C++, i++ is rvalue and ++i is lvalue.
In C++, the statement
int *p1 = &(i++);
will not work as i++ returns an rvalue and you cannot take the address of an rvalue.
On the other hand, the statement
int *p2 = &(++i);
is fine (i.e., it works). Check it out here.
i++ yields the value of i before being incremented and increments it.
If &(i++) were valid, it would mean take the address of the "previous value". Since i is the name we are using for a single memory location, where would that previous value go?
If you need the previous value of i, it needs to be stored somewhere before you can do anything with it including pointing to it.
int j = i++;
int *ip = &i;
int *jp = &j;
Then, *ip will yield the incremented value and *jp will yield the value before the increment.
Since ++i yields the incremented value of i, it works with just a single value. Still, for clarity, best to use separate statements for the increment and taking the address of the variable:
int *ip = &i;
++i;
even if the compiler allows &(++i) or even if it is legal (did not check). You are writing for the fellow programmer (who might be you), not the compiler.

Operator++: reference vs value return and unused argument

I saw this C++ code as part of a larger example:
Date &Date::operator++()
{
helpIncrement();
return *this;
}
Date Date::operator++( int )
{
Date temp = *this;
helpIncrement();
return temp;
}
Firstly if Date temp = *this, then I dont see why the return type is any different for these two functions? One returns *this, the other returns temp, which is assigned to *this?
Secondly, why does the parameter for the second function not have a variable name?
The first returns the object pointed to by this as a reference. That is, the returned object is the object that operator++ is being called on. However, when you do Date temp = *this, temp is copy constructed from the value of *this. It is in turn then copied out of the function. What you get from the second function is a whole new object. Why the functions have this difference is explained in the answer to your second question.
There are two types of increment operator - one is post-increment (i++) and the other is pre-increment (++i). To be able to overload each of them individually (despite them having the same name, operator++), the C++ standard specifies that the post-increment operator takes an argument of type int with unspecified value. This is simply so that you can overload the function for each use of the operator. Since you're unlikely to want to use that unspecified value, you may as well just leave it unnamed.
Now, the expected behaviour of the pre-increment operator is that it increments the object and evaluates to then be that object. That's why it returns a reference in this case. The expected behaviour of post-increment is that it keeps a copy of the original value, increments the object and then returns the original value. Hence, it returns the temp copy.
This is an excellent question - it highlights a rather silly design choice the designers of C++ took:
The overload with no arguments is pre-increment; the overload with an unused int parameter is post-increment.
That is why the pre-increment version can return a reference, while the post-increment must make a copy of the original before incrementing the value itself.
The int parameter is added for a single reason - distinguishing between the two overloads; it is otherwise meaningless, that is why its value is never used.
The first operator is pre-increment: it increments the value, and returns the result. The second is post-increment: it increments the value, and returns the previous value. In that code, temp holds the previous value, helpIncrement() increments the value, and the previous value (temp) is returned. The reason the argument doesn't have a name is that it isn't used. That's a bit of a hack; the compiler knows that ++my_value should get translated into my_value.operator++(), and that my_value++ should get translated into my_value.operator++(1). That is, the absence or presence of an integral argument determines which overload to call.
The first function returns reference to this, after it was incremented. The second one returns a copy of this, before it was incremented.
The unused parameter in the second function distinguishes between prefix (no parameter) and postfix (single int parameter) versions of ++ operator.
This is a basic topic, read about overloading operator++. You can start here: Operator overloading
Firstly if Date temp = *this, then I dont see why the return type is any different for these two functions?
Let's compare this with the situation of ++ on good old int. Consider
int i = 1;
int j = i++;
After this, j holds the old value of i, but i itself is incremented. So i must have been copied prior to the increment, as if ++ on int were defined as
class int { // pseudocode
public:
int operator++(int)
{
int temp = *this;
*this += 1;
return temp;
}
};
OTOH, after
int i = 1;
int j = ++i;
i and j have the same value, so ++ must have been implemented as
int &operator()
{
*this += 1;
return *this;
}
where the change from int to int& introduces convenience: there's no need to make a copy and it's possible to use ++i in a situation where a reference is needed.
Secondly, why does the parameter for the second function not have a variable name?
Because it should never be used. The argument is there as a syntactic gimmick so the compiler can distinguish the two types of operator++ (pre- and post-increment) from each other, but it doesn't have any well-defined value. Giving it a name would trigger an "unused identifier" option in a compiler with proper warnings enabled.

Is `*--p` actually legal(well formed) in C++03

I'm wondering about this sample piece of code:
int main()
{
char *p ;
char arr[100] = "Hello";
if ((p=arr)[0] == 'H') // do stuffs
}
Is this code actually well formed in C++03?
My argument is that the side effect of = is completed only after the next sequence point and since we are accessing the result of p=arr the code might not be well formed, there is no ordering between = and [] operations.
Am I correct?
The behavior is well defined in C and in C++11. This code is actually derived from MySQL.
Of course it's well-defined.
It doesn't matter when the assignment p=arr takes place. You aren't evaluating p[0], you're subscripting the result of (p=arr), which is the pointer value which is being stored into p. Whether or not it's been stored yet doesn't change the value, and the value is known irrespective of whether p has been modified yet.
Similarly, in *--p, there's no undefined behavior. There'd only be undefined behavior if the same variable was accessed twice, including at least one write, between sequence points. But p is only accessed once, as part of --p. It isn't read again (*p), the dereferencing operator is applied to the result of --p which is a well-defined pointer value.
Now, this would be undefined behavior:
void* a;
void* p = &a;
reinterpret_cast<void**>(p = &p)[0] = 0;
as would
int *pi = new int[5];
int i = **&++pi;
It should be clear that the result of a preincrement is not a read unordered with the write, because to assert that there is a race is to assert that ++p can never be used as an rvalue, in which case it must stand alone between sequence points, and post-increment could be used instead. There would be no advantage to having both pre-increment and post-increment in the language.

Reference in lhs and rhs difference in C++

I am learning C++ and I found that when a reference is on the right hand side, there can be two cases. Suppose I have a method:
int& GetMe(int& i) { return i; }
And I have:
(1) int j; GetMe(j) = GetMe(i);
and
(2) int& k = GetMe(i);
The consequences of (1) and (2) are different. In (1), the semantic is to copy the value of i into j. The addresses of i and j are remained the same. Changing i doesn't affect j at all. Actually this is the case when you overload the index operator[] and use the index operator for assignment. In (2), the semantic is to create a referent of i as k. k has the same address as i and changing i affects k.
So why do we have the differences? I think, in C++, a reference's address is determined once and only once. Once a reference's address is determined, it can never be changed later. In (1), the reference of j is determined before, so the action is to copy value from i to j. In (2), the reference of k is being declared and initialized so it is done using reference of i. So the action is reference initialization.
I didn't find material explicitly saying above things so I want confirmation. Anyone knows reference well in C++ must can help me or point me to clear material. Thank you very much in advanced.
What you are missing here is that type of variable is different and it is all that matters. In first example you have int j and in second - int &k.
References in functions prototypes exists in different grounds, they looks the same, underneath they are pretty much the same but they used differently because they exists only when method is executing.
Actually, your code is dong exactly following
int j;
int & j1 = j;
int & i1 = i;
j1 = i1;
versus
int & i1 = i;
int & k = i1;
It's easy to see that in first case two references actually reference different variables i.e. different parts in memory but in second case they all reference the exact same variable. Hence the difference.
In case 1, GetMe returns a reference to j and you then assign a value to j through that reference. In case 2 GetMe returns a reference to i and then you assign that reference to another reference k meaning k refers to i.
References are immutable in C++ (meaning that you cannot change what a reference refers to, though you can change the value of the referent). Reference are also transparently substitutable for what they refer to when used on the right hand side of an expression. The immutability of references means that there is no ambiguity in 1 - you must intend to assign to what the return value of GetMe refers to rather than to change the reference. There is no ambiguity in 2 because you are assigning a reference to a reference (you can't assign an int to a reference to an int).
The difference is that in your second example you are defining a reference and in the first example, you are assigning to an existing reference.

Why is ++i considered an l-value, but i++ is not?

Why is ++i is l-value and i++ not?
Other people have tackled the functional difference between post and pre increment.
As far as being an lvalue is concerned, i++ can't be assigned to because it doesn't refer to a variable. It refers to a calculated value.
In terms of assignment, both of the following make no sense in the same sort of way:
i++ = 5;
i + 0 = 5;
Because pre-increment returns a reference to the incremented variable rather than a temporary copy, ++i is an lvalue.
Preferring pre-increment for performance reasons becomes an especially good idea when you are incrementing something like an iterator object (eg in the STL) that may well be a good bit more heavyweight than an int.
Well as another answerer pointed out already the reason why ++i is an lvalue is to pass it to a reference.
int v = 0;
int const & rcv = ++v; // would work if ++v is an rvalue too
int & rv = ++v; // would not work if ++v is an rvalue
The reason for the second rule is to allow to initialize a reference using a literal, when the reference is a reference to const:
void taking_refc(int const& v);
taking_refc(10); // valid, 10 is an rvalue though!
Why do we introduce an rvalue at all you may ask. Well, these terms come up when building the language rules for these two situations:
We want to have a locator value. That will represent a location which contains a value that can be read.
We want to represent the value of an expression.
The above two points are taken from the C99 Standard which includes this nice footnote quite helpful:
[ The name ‘‘lvalue’’ comes originally
from the assignment expression E1 =
E2, in which the left operand E1 is
required to be a (modifiable) lvalue.
It is perhaps better considered as
representing an object ‘‘locator
value’’. What is sometimes called
‘‘rvalue’’ is in this International
Standard described as the ‘‘value of
an expression’’. ]
The locator value is called lvalue, while the value resulting from evaluating that location is called rvalue. That's right according also to the C++ Standard (talking about the lvalue-to-rvalue conversion):
4.1/2: The value contained in the object
indicated by the lvalue is the rvalue
result.
Conclusion
Using the above semantics, it is clear now why i++ is no lvalue but an rvalue. Because the expression returned is not located in i anymore (it's incremented!), it is just the value that can be of interest. Modifying that value returned by i++ would make not sense, because we don't have a location from which we could read that value again. And so the Standard says it is an rvalue, and it thus can only bind to a reference-to-const.
However, in constrast, the expression returned by ++i is the location (lvalue) of i. Provoking an lvalue-to-rvalue conversion, like in int a = ++i; will read the value out of it. Alternatively, we can make a reference point to it, and read out the value later: int &a = ++i;.
Note also the other occasions where rvalues are generated. For example, all temporaries are rvalues, the result of binary/unary + and minus and all return value expressions that are not references. All those expressions are not located in an named object, but carry rather values only. Those values can of course be backed up by objects that are not constant.
The next C++ Version will include so-called rvalue references that, even though they point to nonconst, can bind to an rvalue. The rationale is to be able to "steal" away resources from those anonymous objects, and avoid copies doing that. Assuming a class-type that has overloaded prefix ++ (returning Object&) and postfix ++ (returning Object), the following would cause a copy first, and for the second case it will steal the resources from the rvalue:
Object o1(++a); // lvalue => can't steal. It will deep copy.
Object o2(a++); // rvalue => steal resources (like just swapping pointers)
It seem that a lot of people are explaining how ++i is an lvalue, but not the why, as in, why did the C++ standards committee put this feature in, especially in light of the fact that C doesn't allow either as lvalues. From this discussion on comp.std.c++, it appears that it is so you can take its address or assign to a reference. A code sample excerpted from Christian Bau's post:
int i;
extern void f (int* p);
extern void g (int& p);
f (&++i); /* Would be illegal C, but C programmers
havent missed this feature */
g (++i); /* C++ programmers would like this to be legal */
g (i++); /* Not legal C++, and it would be difficult to
give this meaningful semantics */
By the way, if i happens to be a built-in type, then assignment statements such as ++i = 10 invoke undefined behavior, because i is modified twice between sequence points.
I'm getting the lvalue error when I try to compile
i++ = 2;
but not when I change it to
++i = 2;
This is because the prefix operator (++i) changes the value in i, then returns i, so it can still be assigned to. The postfix operator (i++) changes the value in i, but returns a temporary copy of the old value, which cannot be modified by the assignment operator.
Answer to original question:
If you're talking about using the increment operators in a statement by themselves, like in a for loop, it really makes no difference. Preincrement appears to be more efficient, because postincrement has to increment itself and return a temporary value, but a compiler will optimize this difference away.
for(int i=0; i<limit; i++)
...
is the same as
for(int i=0; i<limit; ++i)
...
Things get a little more complicated when you're using the return value of the operation as part of a larger statement.
Even the two simple statements
int i = 0;
int a = i++;
and
int i = 0;
int a = ++i;
are different. Which increment operator you choose to use as a part of multi-operator statements depends on what the intended behavior is. In short, no you can't just choose one. You have to understand both.
POD Pre increment:
The pre-increment should act as if the object was incremented before the expression and be usable in this expression as if that happened. Thus the C++ standards comitee decided it can also be used as an l-value.
POD Post increment:
The post-increment should increment the POD object and return a copy for use in the expression (See n2521 Section 5.2.6). As a copy is not actually a variable making it an l-value does not make any sense.
Objects:
Pre and Post increment on objects is just syntactic sugar of the language provides a means to call methods on the object. Thus technically Objects are not restricted by the standard behavior of the language but only by the restrictions imposed by method calls.
It is up to the implementor of these methods to make the behavior of these objects mirror the behavior of the POD objects (It is not required but expected).
Objects Pre-increment:
The requirement (expected behavior) here is that the objects is incremented (meaning dependant on object) and the method return a value that is modifiable and looks like the original object after the increment happened (as if the increment had happened before this statement).
To do this is siple and only require that the method return a reference to it-self. A reference is an l-value and thus will behave as expected.
Objects Post-increment:
The requirement (expected behavior) here is that the object is incremented (in the same way as pre-increment) and the value returned looks like the old value and is non-mutable (so that it does not behave like an l-value).
Non-Mutable:To do this you should return an object. If the object is being used within an expression it will be copy constructed into a temporary variable. Temporary variables are const and thus it will non-mutable and behave as expected.
Looks like the old value:This is simply achieved by creating a copy of the original (probably using the copy constructor) before makeing any modifications. The copy should be a deep copy otherwise any changes to the original will affect the copy and thus the state will change in relationship to the expression using the object.
In the same way as pre-increment:It is probably best to implement post increment in terms of pre-increment so that you get the same behavior.
class Node // Simple Example
{
/*
* Pre-Increment:
* To make the result non-mutable return an object
*/
Node operator++(int)
{
Node result(*this); // Make a copy
operator++(); // Define Post increment in terms of Pre-Increment
return result; // return the copy (which looks like the original)
}
/*
* Post-Increment:
* To make the result an l-value return a reference to this object
*/
Node& operator++()
{
/*
* Update the state appropriatetly */
return *this;
}
};
Regarding LValue
In C (and Perl for instance), neither ++i nor i++ are LValues.
In C++, i++ is not and LValue but ++i is.
++i is equivalent to i += 1, which is equivalent to i = i + 1.
The result is that we're still dealing with the same object i.
It can be viewed as:
int i = 0;
++i = 3;
// is understood as
i = i + 1; // i now equals 1
i = 3;
i++ on the other hand could be viewed as:
First we use the value of i, then increment the object i.
int i = 0;
i++ = 3;
// would be understood as
0 = 3 // Wrong!
i = i + 1;
(edit: updated after a blotched first-attempt).
The main difference is that i++ returns the pre-increment value whereas ++i returns the post-increment value. I normally use ++i unless I have a very compelling reason to use i++ - namely, if I really do need the pre-increment value.
IMHO it is good practise to use the '++i' form. While the difference between pre- and post-increment is not really measurable when you compare integers or other PODs, the additional object copy you have to make and return when using 'i++' can represent a significant performance impact if the object is either quite expensive to copy, or incremented frequently.
By the way - avoid using multiple increment operators on the same variable in the same statement. You get into a mess of "where are the sequence points" and undefined order of operations, at least in C. I think some of that was cleaned up in Java nd C#.
Maybe this has something to do with the way the post-increment is implemented. Perhaps it's something like this:
Create a copy of the original value in memory
Increment the original variable
Return the copy
Since the copy is neither a variable nor a reference to dynamically allocated memory, it can't be a l-value.
How does the compiler translate this expression? a++
We know that we want to return the unincremented version of a, the old version of a before the increment. We also want to increment a as a side effect. In other words, we are returning the old version of a, which no longer represents the current state of a, it no longer is the variable itself.
The value which is returned is a copy of a which is placed into a register. Then the variable is incremented. So here you are not returning the variable itself, but you are returning a copy which is a separate entity! This copy is temporarily stored inside a register and then it is returned. Recall that a lvalue in C++ is an object that has an identifiable location in memory. But the copy is stored inside a register in the CPU, not in memory. All rvalues are objects which do not have an identifiable location in memory. That explains why the copy of the old version of a is an rvalue, because it gets temporarily stored in a register. In general, any copies, temporary values, or the results of long expressions like (5 + a) * b are stored in registers, and then they are assigned into the variable, which is a lvalue.
The postfix operator must store the original value into a register so that it can return the unincremented value as its result.
Consider the following code:
for (int i = 0; i != 5; i++) {...}
This for-loop counts up to five, but i++ is the most interesting part. It is actually two instructions in 1. First we have to move the old value of i into the register, then we increment i. In pseudo-assembly code:
mov i, eax
inc i
eax register now contains the old version of i as a copy. If the variable i resides in the main memory, it might take the CPU a lot of time to go and get the copy all the way from the main memory and move it into the register. That is usually very fast for modern computer systems, but if your for-loop iterates a hundred thousand times, all those extra operations start to add up! It would be a significant performance penalty.
Modern compilers are usually smart enough to optimize away this extra work for integer and pointer types. For more complicated iterator types, or maybe class types, this extra work potentially might be more costly.
What about the prefix increment ++a?
We want to return the incremented version of a, the new version of a after the increment. The new version of a represents the current state of a, because it is the variable itself.
First a is incremented. Since we want to get the updated version of a, why not just return the variable a itself? We do not need to make a temporary copy into the register to generate an rvalue. That would require unnecessary extra work. So we just return the variable itself as an lvalue.
If we don't need the unincremented value, there's no need for the extra work of copying the old version of a into a register, which is done by the postfix operator. That is why you should only use a++ if you really need to return the unincremented value. For all other purposes, just use ++a. By habitually using the prefix versions, we do not have to worry about whether the performance difference matters.
Another advantage of using ++a is that it expresses the intent of the program more directly: I just want to increment a! However, when I see a++ in someone else's code, I wonder why do they want to return the old value? What is it for?
C#:
public void test(int n)
{
Console.WriteLine(n++);
Console.WriteLine(++n);
}
/* Output:
n
n+2
*/