1

In C++ is it bad to have an undefined variable as a function argument which has a default argument value?

I believe this would lead to undefined behavior but I've seen it in some preexisting code and I don't see any warnings being outputted and wanted to confirm the behavior I should be expecting

Header File - header.h

#ifndef HEADER_H
#define HEADER_H

class aClass
{
    int someFunctionA(int aValue = 0);
    virtual int someFunctionB(int aValue = 0);
}

#endif

CPP File - body.cpp

int aClass::someFunctionA(int aValue)
{
    if (aValue == 0)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int aClass::someFunctionB(int aValue)
{
    if (aValue == 0)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int main(int argc, char **argv, char **envp)
{
    int uninitializedInt;
    int initializedInt = 1;

    aClass example = new aClass();

    aClass.someFunctionA(); // Expecting this to return 0
    aClass.someFunctionB(); // Expecting this to return 0

    aClass.someFunctionA(uninitializedInt); // Expecting this to be undefined behaviour
    aClass.someFunctionB(uninitializedInt); // Expecting this to be undefined behaviour

    aClass.someFunctionA(initializedInt); // Expecting this to return 1
    aClass.someFunctionB(initializedInt); // Expecting this to return 1
}

Are my expectations as per my code comments correct?

Puddler
  • 2,619
  • 2
  • 17
  • 26
  • 1
    `Are my expectations as per my code comments correct?` Yes (I think?). Why should they be wrong? ... Warnings can fail, that's why computers don't program. – deviantfan Jan 12 '16 at 00:35
  • @deviantfan I wasn't getting any warning and I thought it might be some behavior that I was unaware of. For example if you pass an uninitialized variable it would automatically set it to the default value – Puddler Jan 12 '16 at 00:38
  • Your example code is hopelessly broken. I'd expect a compiler to warn about use of the uninitialized variable if you set warning levels high enough. – Praetorian Jan 12 '16 at 00:38
  • @Praetorian: You should turn your compiler warning level up then. How you do that will depend on the compiler. – Martin York Jan 12 '16 at 00:41
  • About the duplicate, does it really explain this here? Couldn't find it until now... OP knows what undefined means in general. – deviantfan Jan 12 '16 at 00:48
  • @deviantfan If I phrased the question a bit better I think it would look less like a duplicate. Something like "Is the behavior of a function undefined if it has a default variable value but is called with an undefined variable" – Puddler Jan 12 '16 at 00:54
  • Uninitialzed variables will have indeterminate value and using an indeterminate value is undefined behavior, see [this question for the latest details with respect to C++](http://stackoverflow.com/q/23415661/1708801) – Shafik Yaghmour Jan 12 '16 at 01:19

1 Answers1

3

Are my expectations as per my code comments correct?

Yes.
Warnings are not guarenteed to be always correct, ie. wrong warnings and missing warnings are possible, essentially because of the halting problem (for some warnings at least. And yes, this case is not that hard, but still...).

deviantfan
  • 11,268
  • 3
  • 32
  • 49