c++/2892
Ian Williams
ianw@sgi.com
Tue May 29 10:56:00 GMT 2001
The following reply was made to PR c++/2892; it has been noted by GNATS.
From: Ian Williams <ianw@sgi.com>
To: "'nathan@gcc.gnu.org'" <nathan@gcc.gnu.org>
Cc: gcc-gnats@gcc.gnu.org, Ian Williams <ianw@sgi.com>,
Keith Seto
<keith@sgi.com>, 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 <string.h>
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
template <class T, class F> // 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<T,F>& b);
int mBufferLength;
bool mOverflowReported;
char* mBufferName;
};
template <class T, class F>
Buffer<T,F>::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 <class T, class F>
Buffer<T,F>::Buffer(const Buffer<T,F>& b) :
mBufferLength(0), mOverflowReported(FALSE),
mBuffer(NULL), mInBuffer(0), mBufferName(NULL)
{
}
template <class T, class F>
Buffer<T,F>::~Buffer()
{
if (mBuffer != NULL)
{
delete [] mBuffer;
mBuffer = NULL;
mBufferLength = 0;
reset();
}
if (mBufferName != NULL)
{
delete [] mBufferName;
mBufferName = NULL;
}
}
template <class T, class F>
T*
Buffer<T,F>::buf() const
{
return mBuffer;
}
template <class T, class F>
int
Buffer<T,F>::bufLength() const
{
return mInBuffer;
}
template <class T, class F>
bool
Buffer<T,F>::canAddTo()
{
if ( mInBuffer == mBufferLength )
{
if (!mOverflowReported)
{
mOverflowReported = TRUE;
if (mBufferName != NULL)
cerr << mBufferName << ": ";
cerr << "Buffer overflow\n";
}
return FALSE;
}
return TRUE;
}
template <class T, class F>
void
Buffer<T,F>:: reset()
{
mOverflowReported = FALSE;
mInBuffer = 0;
}
//////////////// Subclass for buffering arrays /////////////////////////////
template <class T, class F>
class BufferV : public Buffer<T,F>
{
public:
BufferV(int length=100, char* label=NULL);
virtual ~BufferV();
virtual bool add(F item);
private:
BufferV(const BufferV<T,F>& b);
};
template <class T, class F>
BufferV<T,F>::BufferV(int length, char* label) : Buffer<T,F>(length,label)
{
}
template <class T, class F>
BufferV<T,F>::BufferV(const BufferV<T,F>& b) : Buffer<T,F>(b)
{
}
template <class T, class F>
BufferV<T,F>::~BufferV()
{
}
template <class T, class F>
bool
BufferV<T,F>::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<T,F>::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<Dxyz,Dxyz> 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;
}
More information about the Gcc-prs
mailing list