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>.
Related
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?
This question already has answers here:
Why are global variables always initialized to '0', but not local variables? [duplicate]
(4 answers)
Why are global and static variables initialized to their default values?
(5 answers)
Closed 1 year ago.
I decided to change the scope of a function operator from global to local. After changing from the commented code, I found that my code no longer runs and exits with error:
C4700 uninitialized local variable 'n' used.\
This seems to be a quite obvious contradiction to the actual method of local resolution. Does anyone have an explanation for this?
int Combs::factorial(int a)
{
//value = 1;
int n;
for (int i = a; i >0; i--)
{
n *= i;
}
cout << n;
return n;
}
When the variable is declared at global/file scope, the compiler initializes it for you, when it's local to a function, it doesn't, so you need to do it yourself.
n is indeed used unitialized, when it was a global variable it was not.
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:
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.
This question already has answers here:
Loop every 10 second
(7 answers)
Closed 7 years ago.
I'm trying to write a loop that runs once every x milliseconds, is there a way to do something like this effectively in c++?
Hope someone could help me with an example how I would go about writing a loop like this
One and the simplest approach would be using windows.h library's Sleep() function.
#include "windows.h"
...
while(1)
{
for(...) {} // your loop
Sleep(miliseconds);
if(something) { break; } // to prevent infinite looping.
}
...
A better solution would be using std::this_thread::sleep_for() from < thread > header.
more documentation and examples here.