Incrementing index inside subscript operator [duplicate] - c++

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 2 years ago.
I was trying to make a 'smart' optimization when I stumbled upon some weird behavior, so I created a minimal example to replicate it.
In the code below, I create an array and shift elements to the left, then print the array.
The code works exactly like one would expect.
#include <iostream>
int main()
{
int array[5]{0,1,2,3,4};
for (int i = 0; i < 4; ++i)
{
array[i] = array[i + 1]; // Array becomes {1,2,3,4,4}
}
std::cout << "Printing array: ";
for (int i = 0; i < 5; ++i)
{
std::cout << array[i]; // Prints {1,2,3,4, 4}
}
std::cout << "\n";
}
Now, looking at the first for block, I thought I could spare one i increment.
So I tried to rewrite that block as follows:
for (int i = 0; i < 4;)
{
array[i] = array[++i]; // Expected: array becomes {1,2,3,4,4}
}
This does not work, however.
i is in fact updated, because the program does not loop, but the assignment does not go as expected and the terminal outputs Printing array: 01234.
Now, it might have been that the expression on the right is evaluated before the expression on the left, in which case this behavior would be explained.
However, I also tried the following reverse iteration:
for (int i = 4; i > 0;)
{
array[--i] = array[i]; // Expected: array becomes {1,2,3,4,4}
}
And it also outputs Printing array: 01234.
In both cases, I tried compiling in debug mode, in case it was some weird compiler optimization responsible for it, but the result was still the same.

Prior to C++17, it is undefined behavior,
Since C++17, we have
Order of evaluation
20) In every simple assignment expression E1=E2 and every compound assignment expression E1#=E2, every value computation and side-effect of E2 is sequenced before every value computation and side effect of E1.
So
array[i] = array[++i];
array[++i] is evaluated first, (incrementing so i)
array[i] is evaluated after, with i incremented.
So it is mostly equivalent to
++i;
array[i] = array[i]; // Noop
And for
array[--i] = array[i];
it is mostly equivalent to
array[i - 1] = array[i];
--i

You are invoking undefined behavior because the order of evaluation in an expression is unspecified. Don't read and write from/to an object in the same expression. The offending lines are:
array[i] = array[++i];
and:
array[--i] = array[i];
This was changed in the C++17 standard and if you compiled with C++17 support, you would not invoke undefined behavior.

Related

Is i++ or ++i better for this program?

i'm trying to create a program which checks if input is is an int or a string.
This is the code:
// CPP program to check if a given string
// is a valid integer
#include <iostream>
using namespace std;
// Returns true if s is a number else false
bool isNumber(string s)
{
for (int i = 0; i < s.length(); i++)
if (isdigit(s[i]) == false)
return false;
return true;
}
// Driver code
int main()
{
// Saving the input in a string
string str = "6790";
// Function returns 1 if all elements
// are in range '0-9'
if (isNumber(str))
cout << "Integer";
// Function returns 0 if the input is
// not an integer
else
cout << "String";
}
I wanted to ask that whether i++ or ++i is better for this loop and why?
for (int i = 0; i < s.length(); i++)
if (isdigit(s[i]) == false)
return false;
THANK YOU!
I prefer the form ++i in C++, because i may be an iterator or other object with overloaded operator++. In those cases, the form i++ generates a temporary object to hold the previous value of i, while the form ++i does not. The compiler may optimize away that temporary object, but it’s not required to, and in some cases may not be allowed to.
So, ++i is slightly better than i++ as the former need not retain the initial value and recheck it. It is one of the very few instances where time optimization and memory optimization occur simultaneously. But the difference is too small to be noted, just 4 bytes. Also, the time difference is negligibly small.
You would essentially get the same answer in your example but might receive a minute time and memory optimization while using ++i.
Since the datatype of i is int, it doesn't matter if you use i++ or ++i.
If its a large class iterator, ++i is faster than i++. Its a good practice to use ++i.

Is this code undefined behavior due to accessing out of bound element in the first for loop?

Is this code undefined behavior due to accessing out of bound element in the first for loop?
int main() {
std::size_t length = 4;
int* a = new int[length];
for (std::size_t i = 0; i < length; )
a[i] = ++i;
for (std::size_t i = length - 1; i >= 0; i--)
std::cout << a[length - 1] << " ";
}
If shrotly, yes, it is. In this situation C++ doesn't define in which order expression must be calculated, because both sides have side effects. So one compiler can firstly calculate left side, second can calculate at first right side. But if you look at this, you can find that
a[i] = i++; // undefined behavior until C++17
So, you can use this line, only if you use C++ 17.

What is the difference between these two assignments?

Here is one way of assigning value to an array
char s[2][2];
s[0][0] = 1;
s[0][1] = 2;
s[1][0] = 3;
s[1][1] = 4;
cout << s[0][2];
output of this will be some garbage and it keeps changing. But strange thing happens when I do this
char s[2][2];
for (int i = 0, j = 0; i <= 2; i++){
if(i == 2)
{
j++;
i = 0;
}
if(j == 2) break;
cin >> s[j][i];
}
cout << s[0][2];
so as I give input as
1 2
3 4
the output was 3?
First of all shouldn't it complain that we are using array beyond its index and secondly why so much difference?
One of the things that makes C and C++ fast is that it doesn't check if you go past the bounds of an array.
Instead you get undefined behavior. When you trigger this, anything can happen. Your program may crash, it may appear to work properly, or it may generate strange results.
First of all shouldn't it complain that we are using array beyond its index
No. The standard does not specify that a program should complain if you use an array out of bounds. The standard specifies that the behaviour is undefined.
secondly why so much difference?
The behaviour is undefined. In both cases.

Increment ++i, i++ and i+=1

I am beginner in C++. What I understand is that:-
i++ is executing first, then increment, ++i is increment first, then execute,i+=1 is increment by 1,then execute. But in the FOR loop:
for (i=0;i<10;i++)
for (i=0;i<10;++i)
There is really no difference in these two loops above.
Here is another one to calculate the sum of all integers from 1 to 100:
int i=1, sum=0;
while (i<=100)
{
sum+=i;
i++; //i+=1; ++i;
}
cout<<sum<<" "<<i<<endl;
return 0;
But if I replace i++ with i+=1 or ++i, they all return a sum of 5050 and i of 101. So I really don't see any difference in them.
So could anyone explain this to me? Which one of those is used most in programming? Thank you!!
You are correct. In your examples there is no difference.
But here there is:
int i = 0;
cout << i++ << endl; //prints 0
cout << i << endl; //prints 1
vs
int i = 0;
cout << ++i << endl; //prints 1
cout << i << endl; //prints 1
Which one of those is used most in programming?
Most of the time, ++ is the only operation in the statement (FYI, a for loop has three statements).
If it isn't, then it might matter, and you'll use whichever one gives you the correct behavior.
FYI
Some developers have the opinion that if pre and postfix operators should always be used alone (not part of a large statement). They can lead to confusing code, or even undefined behavior.
For example,
int i = 0;
cout << i++ + i++ << endl;
has undefined behavior.
So could anyone explain this to me?
What i++ does is return the current value of i and then increment it by one, and ++i first increment i by 1 and then returns the value of i.
Take a look at this, for example:
i = 5;
j = 5;
res = i++; //res = 5, but i=6
res = ++j; //res = 6 and j=6
Which one of those is used most in programming?
Both are used depending on what you want or may be how you want.
One more thing to note:
++i is an l-value, but i++ is not. For details see here
Your analysis is correct. i++ will return the value of i, then increment, whereas ++i will increment the value of i, then return the new value. i += 1 will do the same as ++i. The difference in where they will be used in actual code is primarily situational; there's no specific answer as to where each of them are most often used or helpful. It all depends on what you want to do.
Here's a contrived example of one time it might be useful to use post-increment (i++ form):
// Return the length of a string, including the null byte
int stringLength(char str[])
{
int length = 0;
while (str[length++] != 0);
return length;
}
If you wanted to return the length without the null byte, you could modify the above example slightly, using the ++i form:
// Return the length of a string, without the null byte
int stringLength(char str[])
{
int length = -1;
while (str[++length] != 0);
return length;
}
As for i += 1, I don't think I've ever done quite that, since you can use pre- or post-increment instead. I've generally only used the compound assignment operators for values other than 1.
I think if you imagine how the for loop works you can understand the problem at hand.
for loop
initialization --> i = 0
<check condition> --> i<10? ------->--------------------
| | |
^ |yes(i.e condition not met) |no(i.e condition met)
| V V
| --------------
| |body of loop|
| --------------
| |
| V
-------<------------increment i (**) exit for loop
** increment i means i++ or ++i
i++ can be replaced by this :
int temp = i;
i = i + 1;
temp will be useless here so the compiler will optimize it to just inc i instruction. even if it doesn't do that temp is just a waste of space that's all.
++i can be replaced by
i = i + 1;
int temp = i;
again temp is not required so the compiler will just replace it with inc i instruction.
if you see both the instruction are the same because they are not being assigned to anything. only i is being affected by the increment. so both are essentially the same. this is true if i is a built-in type .
you see that the increment instruction is placed after the body of the loop? can you now see that this is almost similar to the while loop that you showed?
it is always nice to think of loops in this way.

Does the following code invoke Undefined Behavior?

#include <iostream>
#include <cmath>
#define max(x,y) (x)>(y)? (x): (y)
int main() {
int i = 10;
int j = 5;
int k = 0;
k = max(i++,++j);
std::cout << i << "\t" << j << "\t" << k << std::endl;
}
No, it doesn't.
In this case the situation is saved by the fact the ?: operator has a sequence point immediately after evaluating the first operand (the condition) and after that only one of the two expressions (second or third operand) is evaluated. Your code is equivalent to
...
bool c = i++ > ++j;
k = c ? i++ : ++j;
...
No undefined behavior here.
Well, there certainly are a lot of problems with it.
max is actually computing min
increment operators are doubled on whatever choice is selected since you are using a macro
using postfix/prefix increments is just thrown in to confuse, but doesn't have a lot of bearing on the problem.
This code will produce the same results each time run, so no, it's not undefined. At the cout:
i = 11
k = 7
j = 7
This sounds like a bad homework problem. :)