Article View: gnu.g++.bug
Article #132g++ 2.1 problem
From: Joe Linoff
Date: Thu, 09 Apr 1992 21:30
Date: Thu, 09 Apr 1992 21:30
100 lines
2302 bytes
2302 bytes
* VERSION: g++ version 2.1
* SOURCE FILE: problem.cc
=======================================================================
#
# makefile
#
C++=g++
O=problem.o
.cc.o:
$(C++) -c -g $*.cc
problem: $O
$(C++) -o $@ $O
//
// problem.cc
//
#include <iostream.h>
/*
This program should print out the following:
Base
Base
Base
Base
instead, it prints out
Base
Base
*/
class Base {
public:
virtual const char* name() const; // if not virtual,
// the problem goes away
};
const char* Base::name() const { return "Base"; }
class BaseSeq {
Base** data;
unsigned _allocated;
unsigned _size;
public:
BaseSeq(unsigned sz) : _allocated(sz), _size(0) {
data = new Base*[_allocated];
}
Base*& operator[](unsigned i);
BaseSeq& operator+=(Base* p) {
data[_size++] = p;
return *this;
}
};
Base*& BaseSeq::operator[](unsigned i) {
return data[i];
}
void doit() {
BaseSeq seq(4);
seq += new Base;
seq += new Base;
seq += new Base;
seq += new Base;
//
// If you make BaseSeq::data public and then access it directly
// instead of through BaseSeq::operator[], the problem goes away.
// Making operator[] inline does not help.
//
unsigned i = 0;
while (i < 4)
cout << seq[i++]->name() << '\n';
}
int main() {
doit();
return 0;
}
=======================================================================
* COMMAND ARGUMENTS:
g++ -c -g problem.cc
g++ -o problem problem.o
* tm.h and md
tm.h -> ./config/tm-sparc.h
md -> ./config/sparc.md
* MACHINE TYPE and O/S
SPARCstation 2
4.1.1
* DESCRIPTION OF INCORRECT BEHAVIOR:
This program should print out the following:
Base
Base
Base
Base
instead, it prints out
Base
Base
Technical Details
Message-ID:
<9204092130.AA12750@dharna.crosscheck>Path:
arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!dharna.UUCP!joeShow Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!dharna.UUCP!joe From: joe@dharna.UUCP (Joe Linoff) Subject: g++ 2.1 problem Message-ID: <9204092130.AA12750@dharna.crosscheck> Sender: gnulists@ai.mit.edu Organization: GNUs Not Usenet Distribution: gnu Date: Thu, 9 Apr 1992 21:30:15 GMT Approved: bug-g++@prep.ai.mit.edu Lines: 103