Arista Networks C++ Debugging Technical Interview Experience
Interview Experience
The interview involved a debugging problem requiring the analysis of the following C++ code snippet: ```cpp #include
Full Details
The interview involved a debugging problem requiring the analysis of the following C++ code snippet: cpp #include <iostream> using namespace std; void function(int *p) { int x = 100; p = &x; } int main() { int a = 10; int *ptr = &a; function(ptr); cout << "Value: " << *ptr << endl; return 0; } The output of this code is Value: 10. This occurs because the pointer p is passed to the function by value. The function creates a local copy of the pointer, so when p is reassigned to the address of x (p = &x), only the local copy is modified. The original pointer ptr in main remains unchanged and continues to point to a. To achieve the intended result of printing 100, the function must be modified to dereference the pointer and assign the value directly to the memory location, as shown below: cpp #include <iostream> using namespace std; void function(int *p) { int x = 100; *p = x; // Assigns the value of x to the memory location p points to } int main() { int a = 10; int *ptr = &a; function(ptr); cout << "Value: " << *ptr << endl; return 0; }