optimization/6793: g++3.1 bug in optimization of ellipsis
Andrea Latina
andrea.latina@to.infn.it
Fri May 24 00:56:00 GMT 2002
>Number: 6793
>Category: optimization
>Synopsis: varargs constructor inlined inappropriately.
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: rejects-legal
>Submitter-Id: net
>Arrival-Date: Fri May 24 00:56:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator: Andrea Latina
>Release: 3.1
>Organization:
>Environment:
System: Linux to427xl.to.infn.it 2.4.18-4smp #1 SMP Thu May 2 18:32:34 EDT 2002 i686 unknown
Architecture: i686
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../gcc-3.1/configure --enable-threads
>Description:
In the following source I use the ellipsis to pass a variable number of
arguments to the constructor of a N-dimensional vector. When I compile
this source with optimization O1 or O2, I obtain an error:
test.cc: In function `int main()':
test.cc:24: `va_start' used in function with fixed args
when I use any other optimization (or no optimization at all) it is
compiled correctly. Using gcc-3.0.4 this problem doesn't appear.
>How-To-Repeat:
Try compiling this test with and without O2:
#include <iostream>
#include <cstdarg>
#include <cstddef>
#include <cmath>
template <class Type> class VectorNd {
size_t size;
Type *data;
public:
VectorNd(const VectorNd &v ) : size(v.size) { data = new Type[size]; for (size_t i = 0; i < size; i++) data[i] = v.data[i]; }
VectorNd(size_t _size, Type *_data ) : size(_size) { data = new Type[size]; for (size_t i = 0; i < size; i++) data[i] = _data[i]; }
VectorNd(size_t _size, Type arg ) : size(_size) { data = new Type[size]; for (size_t i = 0; i < size; i++) data[i] = arg; }
VectorNd(size_t _size = 1 ) : size(_size) { data = new Type[size]; }
VectorNd(size_t _size, size_t count, ... ) : size(_size)
{
data = new Type[size];
va_list ap;
va_start(ap, count);
for (size_t i = 0; i < count; i++) data[i] = va_arg(ap, Type);
va_end(ap);
}
~VectorNd() { delete []data; }
const VectorNd &operator = (const VectorNd &v )
{
if (this != &v)
{
if (size != v.size)
{
delete []data;
data = new Type[size = v.size];
}
for (size_t i = 0; i < size; i++) data[i] = v.data[i];
}
return *this;
}
friend Type abs(const VectorNd &v )
{
Type sum = v.data[0] * v.data[0];
for (size_t i = 1; i < v.size; i++) sum += v.data[i] * v.data[i];
return sqrt(sum);
}
friend std::ostream &operator << (std::ostream &stream, const VectorNd &v )
{
stream << v.data[0];
for (size_t i = 1; i < v.size; i++) stream << ", " << v.data[i];
return stream;
}
};
int main()
{
VectorNd<double> vector;
vector = VectorNd<double>( 3, // number of components
3, // number of components passed through "..." (must be <= of the previous parameter)
1.0, 2.0, 3.0);
std::cout << "abs(" << vector << ") = " << abs(vector) << std::endl;
return 0;
}
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the Gcc-bugs
mailing list