Question
Hello, I need some help with this C++ task - Code provided below First, look at the code by hand and try to identify vulnerabilities
Hello, I need some help with this C++ task - Code provided below
First, look at the code by hand and try to identify vulnerabilities around memory and concurrency.
- Where is this code vulnerable to memory issues? What impact could that have on the results?
- Where is this code vulnerable to concurrency issues? What impact could that have on the results?
Now, run the code and try to exploit the vulnerabilities you identified above.
- What input are you able to enter into the program to cause a memory issue?
ADD YOUR SCREENSHOT OF THE OUTPUT SHOWING THE RESULTS FROM THE MEMORY VULNERABILITY:
- What input are you able to enter into the program to cause a concurrency issue?
ADD YOUR SCREENSHOT OF THE OUTPUT SHOWING THE RESULTS FROM THE CONCURRENCY ISSUE:
Improve the Code
Attempt to fix the issues you identified. Re-run the program and validate that you can no longer reproduce the memory and concurrency issues you found before.
- What did you correct in the code to address the issues?
#include #include #include using namespace std; int balance = 100; void change_balance(int id){ usleep(id * 100); balance = id * 10; cout << " Thread " << id << " changed balance to " << balance << endl; } int main() { int size; cout << "Enter size: "; cin >> size; int* numbers = new int[size]; for (int i = 0; i < size; i++){ numbers[i] = (i+1) * 2; } for (int i = 0; i < size; i++){ cout << numbers[i] << " "; } const size_t thread_size = 5; thread threads[thread_size]; for (size_t i = 0; i < thread_size; i++){ threads[i] = std::thread(change_balance, i); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started