Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local Variables Being Passed ( C++)

I have encountered a problem in my learning of C++, where a local variable in a function is being passed to the local variable with the same name in another function, both of these functions run in main().

When this is run,

#include <iostream>
using namespace std;

void next();
void again();

int main()
{
    int a = 2;
    cout << a << endl;
    next();
    again();
    return 0;
}

void next()
{
    int a = 5;
    cout << a << endl;
}

void again()
{
    int a;
    cout << a << endl;
}

it outputs:

2
5
5

I expected that again() would say null or 0 since 'a' is declared again there, and yet it seems to use the value that 'a' was assigned in next().

Why does next() pass the value of local variable 'a' to again() if 'a' is declared another time in again()?

like image 415
Jeffrey Malone Avatar asked Mar 23 '16 02:03

Jeffrey Malone


People also ask

Can we pass address of local variable in C?

Yes it is safe to pass a pointer to a local variable but you can't return a pointer to an automatic local variable from a function.

Are variables passed by value in C?

C always uses 'pass by value' to pass arguments to functions (another term is 'call by value', which means the same thing), which means the code within a function cannot alter the arguments used to call the function, even if the values are changed inside the function.

Can local variables be passed by reference?

As we saw above, we can use pass by reference to modify local variables of a function.

What happens to local variables in C?

A local variable is declared within the function or program block, and it can be used inside the code block or subroutine in which it's declared. Local variable exists until the code of block execution; once the code executes, it destroys automatically.


2 Answers

http://en.cppreference.com/w/cpp/language/ub

You're correct, an uninitialized variable is a no-no. However, you are allowed to declare a variable and not initialize it until later. Memory is set aside to hold the integer, but what value happens to be in that memory until you do so can be anything at all. Some compilers will auto-initialize variables to junk values (to help you catch bugs), some will auto-initialize to default values, and some do nothing at all. C++ itself promises nothing, hence it's undefined behavior. In your case, with your simple program, it's easy enough to imagine how the compiler created assembly code that reused that exact same piece of memory without altering it. However, that's blind luck, and even in your simple program isn't guaranteed to happen. These types of bugs can actually be fairly insidious, so make it a rule: Be vigilant about uninitialized variables.

like image 147
mock_blatt Avatar answered Oct 11 '22 20:10

mock_blatt


An uninitialized non-static local variable of *built-in type (phew! that was a mouthful) has an indeterminate value. Except for the char types, using that value yields formally Undefined Behavior, a.k.a. UB. Anything can happen, including the behavior that you see.

Apparently with your compiler and options, the stack area that was used for a in the call of next, was not used for something else until the call of again, where it was reused for the a in again, now with the same value as before.

But you cannot rely on that. With UB anything, or nothing, can happen.


* Or more generally of POD type, Plain Old Data. The standard's specification of this is somewhat complicated. In C++11 it starts with §8.5/11, “If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value.”. Where “automatic … storage duration” includes the case of local non-static variable. And where the “no initialization” can occur in two ways via §8.5/6 that defines default initialization, namely either via a do-nothing default constructor, or via the object not being of class or array type.

like image 32
Cheers and hth. - Alf Avatar answered Oct 11 '22 20:10

Cheers and hth. - Alf