What will be printed out as a result of program execution?
#include <iostream>

using namespace std;

void init(int *x)
{
    x = new int;
    *x = 50;
}

int main()
{
    int v = 100;
    init(&v);
    cout << v;

    return 0;
}
Explanation
Since the pointer is passed to the void init (int *x) function by value, it points to another memory location where number 50 is written after its change in the line x = new int. As a result, the memory allocated by the init function leaks and 100 is displayed on the screen.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes