[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#204580: g++: unexpected output using variadic functions in classes



Package: g++
Version: 3:3.3-2
Severity: normal



-- System Information
Debian Release: 3.0
Architecture: i386
Kernel: Linux suffix 2.4.21 #1 Fri Aug 1 13:53:33 CEST 2003 i686
Locale: LANG=C, LC_CTYPE=C

Versions of packages g++ depends on:
ii  cpp                           3:3.3-2    The GNU C preprocessor.
ii  g++-3.3                       1:3.3.1-1  The GNU C++ compiler
ii  gcc-3.3                       1:3.3.1-1  The GNU C compiler


Hi,
    The following little program produces unexpected output. As an
alternate compiler (as a check I used the Intel C++ compiler version 7.0) did
not produce the problem, I assume I ran into a little bug; hence the
bugreport. The problem is described in some detail beyond the program's
source: 

=====================================================================
#include <iostream>
#include <cstdarg>

using namespace std;

class Foo
{
    va_list list;

    public:
        Foo(int n, ...)
        {   
            va_start(list, n);
        }
        void bar() const
        {
            va_list l(list);
            cout << "Saw " << va_arg(l, int) << endl;
        }
};

void two(int start, Foo foo)
{
    for (int idx = 0; idx < 2; idx++)
        foo.bar();
}

void three(int start, int end, Foo foo)
{
    for (int idx = 0; idx < 2; idx++)
        foo.bar();
}

int main(int argc, char **argv)
{
    Foo foo(3, 15);

    two(2, foo);
    three(2, 2, foo);      // unexpected output from here.
    two(2, foo);

    return 0;
}
==================================================================

Problem description:

The clas `Foo' has a constructor accepting variable numbers of arguments. It
initializes a `va_list' data member which is used to initialize a local
`va_list' variable inside its `bar()' function. The `bar()' function then
prints the first of the variable arguments, which is, in this program, always
an `int' value.

The function `two()' receives two value-type arguments: an `int' and a `Foo'
object: the `Foo' object is therefore copied internally to initialize the
parameter `foo'. The `int' argument is not used, and `foo.bar()' is called
twice.

The function `three()' receives three value-type arguments, two `int's and a
`Foo' object. Both `int' arguments are not used, and `foo.bar()' is called
twice, like in the function `two()'.

In `main()' a `Foo' object is constructed, receiving 2 arguments. The second
value, 15, is the `int' to be accessed by `Foo::bar()'.

At the first call of `two()', 15 is displayed, as expected. Then, unexpected
values are displayed by the call of `three()'. Therafter, the problem
persists: calling `two()' for the second time now also produces unexpected
values.

On my computer I get the following output:

Saw 15
Saw 15
Saw -1073743160
Saw -1073743160
Saw -1073743160
Saw -1073743160

As mentioned, when the program is compiled by, e.g., the Intel V 7.0 compiler 
the problem does not appear. In that case, all displayed values are 15, as
expected. 

Hopefully the problem can be solved. If I can assist, please let me know.

Kind regards,

Frank B. Brokken.




Reply to: