This question already has answers here:
Declaring and initializing a variable in a Conditional or Control statement in C++
(9 answers)
Defining a variable in the condition part of an if-statement?
(5 answers)
Closed 1 year ago.
When cleaning up some code that I have found online, I came across with this weird c++ line:
if (int i = 1) std::cout << i;
With LLVM it compiled fine and the console output is 1, but how does the scope is handled in here, shouldn't the i variable be only accessible inside the conditional (inside the parenthesis)? And how is that possible to be evaluated to true, isn't an assignment a void operation and with no value, so 0/false? What is going on with this line?
Related
This question already has answers here:
Race condition when incrementing and decrementing global variable in C++
(3 answers)
Closed 5 months ago.
Two threads are executing a function named job, inside which they are incrementing a global variable. Will there be a race condition here?
int i = 0;
void *job(void *args)
{
i += 1;
}
Yes, this might result in parallel access to the variable, which is a problem. To avoid that, it's recommended to declare i as std::atomic<int>.
This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
indexing past the end of C arrays [duplicate]
(2 answers)
Why doesn't my program crash when I write past the end of an array?
(9 answers)
Array overflow (why does this work?) [duplicate]
(5 answers)
The effects of writing past the end of an array [duplicate]
(5 answers)
Closed 2 years ago.
I was debugging my code for key index counting and found this problem.
I do not understand what is happening here. I've looked at the code for too long to see if I am missing something very obvious, but it does not seem like it.
int main()
{
const int r=7,len=10;
int arr[10]={1,4,6,2,0,4,3,6,5,2};
int count[r+1]={0};
for(int i=0;i<len;i++)
{
count[arr[i]+1]++;
}
cout<<arr[0]<<" ";
for(int i=0;i<r+1;i++)
{
count[i+1]+=count[i];
}
cout<<arr[0]<<" ";
return 0;
}
This is the kind of a mock up code which generates the same bug.
Output:-
1 11
I am not changing value of arr anywhere in my program and still it shows 11 instead of 1 in the output.
if I comment out count[arr[i]+1]++; or count[i+1]+=count[i]; or both it gives the correct output.
1 1
What is happening please explain. (comment if I am doing something silly).
Edit: This is only happening with arr[0].
Compiling it with g++ -Wall -Wextra you get this warning:
rando_so.cpp: In function 'int main()':
rando_so.cpp:15:19: warning: iteration 7 invokes undefined behavior [-Waggressive-loop-optimizations]
count[i+1]+=count[i];
~~~~~~~~~~^~~~~~~~~~
rando_so.cpp:13:18: note: within this loop
for(int i=0;i<r+1;i++)
This hints you to look a bit closer at that second loop. Your variable i goes up to the highest possible index of count - and then you add 1. That is undefined behaviour. In your case, it is likely that you happen to be writing into the first element of arr now, because of how it's laid out on the stack. But as far as I know, anything could happen as a result of this.
This question already has answers here:
What is a lambda expression in C++11?
(10 answers)
Strange bracket-parentheses notation in C++, looking somewhat like a for each loop
(3 answers)
What does "[ this ]" mean in C++
(1 answer)
Closed 2 years ago.
I'm going through Wt web framework tutorial, in one example they initialize a variable this way:
auto greet = [this]{
greeting_->setText("Hello there, " + nameEdit_->text());
};
What does square brackets mean in this type of initialization?
This question already has answers here:
C++, ternary operator and cout
(2 answers)
Conditional operator used in cout statement
(3 answers)
Closed 2 years ago.
When I write
std::cout << 5 ? 'a' : 'b';
the output is 5? Why should I put the whole expression in parentheses? isn't the ternary operator as a whole an expression and should be evaluated first then output the result? And if std::cout consider that I wanna print the condition, what happens to the part after the condition? Why is it ignored?
This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
What happens when a function that returns an object ends without a return statement
(3 answers)
Closed 4 years ago.
In a test, one of my classmates wrote the following function, to flip a number:
int tukor(int n)
{
int k=0;
while(n!=0)
{
k=k*10+n%10;
n=n/10;
}
n=k;
}
You will notice a complete lack of any return statements, but when cout<<tukor(1234); is run (namespaaace std is used), it outputs 4321. Now the entire class is confused as to how this is possible, even after the teacher added the lines n=0;n=12; at the end of the function. It has worked for all test cases so far.
Is this caused by undefined behavior or something similar?
EDIT: changing k before the n=k statement changes the return value, and if it is removed, the return value is 0.