From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ian Williams To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org Subject: RE: c++/2892 Date: Tue, 29 May 2001 10:56:00 -0000 Message-id: <20010529175601.3983.qmail@sourceware.cygnus.com> X-SW-Source: 2001-05/msg01007.html List-Id: The following reply was made to PR c++/2892; it has been noted by GNATS. From: Ian Williams To: "'nathan@gcc.gnu.org'" Cc: gcc-gnats@gcc.gnu.org, Ian Williams , Keith Seto , nobody@gcc.gnu.org Subject: RE: c++/2892 Date: Tue, 29 May 2001 10:54:02 -0700 Apologies for replying all, however, i'm not sure how to replace the source file without creating a new bug so i've attached the corrected example program source at the bottom of this mail. (I think the original one got messed up as a result of some transfer to/from my Windows based mail system). Let me know if you would prefer me to send the program via another method. Thanks, Ian. -----Original Message----- From: nathan@gcc.gnu.org [ mailto:nathan@gcc.gnu.org ] Sent: Monday, May 28, 2001 3:15 AM To: gcc-gnats@gcc.gnu.org; ianw@sgi.com; keith@sgi.com; nobody@gcc.gnu.org Subject: Re: c++/2892 Synopsis: array addresses corrupted when using template classes State-Changed-From-To: open->feedback State-Changed-By: nathan State-Changed-When: Mon May 28 03:14:57 2001 State-Changed-Why: the sample code provided is full of strang \par things. please proved corrected sample code http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=2892&database=gcc -------------------------------------------------------------- /* Compile: g++ -o test test.c++ */ #include #include #include #include #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif template // T="To" the storage format for the buffer // F="From", the storage format from which the // data comes class Buffer { public: Buffer(int length=100, char* label=NULL); virtual ~Buffer(); virtual bool add(F item) = 0; T* buf() const; int bufLength() const; bool canAddTo(); void reset(); protected: T* mBuffer; int mInBuffer; private: Buffer(const Buffer& b); int mBufferLength; bool mOverflowReported; char* mBufferName; }; template Buffer::Buffer(int length, char* label) : mBufferLength(length), mOverflowReported(FALSE), mBuffer(NULL), mInBuffer(0), mBufferName(NULL) { mBuffer = new T[length]; if (label != NULL) { mBufferName = new char[strlen(label)+1]; strcpy(mBufferName,label); } } template Buffer::Buffer(const Buffer& b) : mBufferLength(0), mOverflowReported(FALSE), mBuffer(NULL), mInBuffer(0), mBufferName(NULL) { } template Buffer::~Buffer() { if (mBuffer != NULL) { delete [] mBuffer; mBuffer = NULL; mBufferLength = 0; reset(); } if (mBufferName != NULL) { delete [] mBufferName; mBufferName = NULL; } } template T* Buffer::buf() const { return mBuffer; } template int Buffer::bufLength() const { return mInBuffer; } template bool Buffer::canAddTo() { if ( mInBuffer == mBufferLength ) { if (!mOverflowReported) { mOverflowReported = TRUE; if (mBufferName != NULL) cerr << mBufferName << ": "; cerr << "Buffer overflow\n"; } return FALSE; } return TRUE; } template void Buffer:: reset() { mOverflowReported = FALSE; mInBuffer = 0; } //////////////// Subclass for buffering arrays ///////////////////////////// template class BufferV : public Buffer { public: BufferV(int length=100, char* label=NULL); virtual ~BufferV(); virtual bool add(F item); private: BufferV(const BufferV& b); }; template BufferV::BufferV(int length, char* label) : Buffer(length,label) { } template BufferV::BufferV(const BufferV& b) : Buffer(b) { } template BufferV::~BufferV() { } template bool BufferV::add(F item) { cout << "item = " << item[0] << ", " << item[1] << ", " << item[2] << endl; /* For some reason the following line yields the correct output when instantiated with F = int[3] or F = float[3] cout << "item = " << item[0+3] << ", " << item[1+3] << ", " << item[2+3] << endl; */ if (Buffer::canAddTo()) { cout << "mBuffer[" << mInBuffer << "] = "; for (int i=0 ; i<3 ; i++) { /* See above when instanciated with F = int[3] and float[3] mBuffer[mInBuffer][i] = item[i+3]; */ mBuffer[mInBuffer][i] = item[i]; cout << mBuffer[mInBuffer][i] << ", "; } cout << endl; mInBuffer++; return TRUE; } return FALSE; } //typedef int Dxyz[3], *DxyzP; //typedef float Dxyz[3], *DxyzP; typedef double Dxyz[3], *DxyzP; static BufferV faceVertexBuf(10,"faceVertexBuf"); Dxyz input_points[4]={ {1.0, -1.0, 1.0},{1.0, 1.0, -1.0}, {1.0, 1.0, 1.0},{1.0, -1.0, 1.0} }; int main (int argc, char* argv[]) { int i = 0, index; faceVertexBuf.reset(); for (i=0;i<4; i++) { index = faceVertexBuf.bufLength(); Dxyz temp = {input_points[i][0], input_points[i][1], input_points[i][2]}; cout << "Input = " << temp[0] << ", " << temp[1] << ", " << temp[2] << endl; ; faceVertexBuf.add(temp); DxyzP tempP = faceVertexBuf.buf()[index]; cout << "Output = " << tempP[0] << ", " << tempP[1] << ", " << tempP[2] << endl; ; } return 0; }