Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions