This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Memory Error in Class valarray



Dear Sirs,

I am sending you a source code that causes a memory error in the 'valarray'
copy constructor.

The attached file 'Matrix.cpp' is the complete C++ code, no other headers or
preprocessing definitions are necessary. I wrote it for both the Borland C++
Builder 5 (console target; see the macro __TURBOC__) and the GNU GCC 2.95.
The Borland compilation of 'Matrix.cpp' results in the following correct
output:

0 2 3
{{11,12,13}, {21,22,23}}
{{7,7}, {7,7}, {7,7}}

whereas the GCC gives:

0 2 3
{{274184,12,13}, {274184,12,13}}
{{273040,7}, {273056,7}, {273056,7}}

I traced down this over-allocation bug to the 'new[]' operator in
'std/std_valarray.h' at line 303. This call corrupts the first members of
'_M_data' vector of argument 'const valarray<_Tp>& __v' of the copy
constructor 'valarray<_Tp>::valarray'.

I am also attaching file 'Matrix.out' generated by the command line
'c++ -ansi -v Matrix.cpp >&! Matrix.out' to show you the specifications of
our environment (gcc version 2.95.2 19991024, SunOS 5.7, SPARC Sun Solaris
2.7).

Please, would you be so kind and let me know when you recognise what is
going on there and how to correct it? Thank you very much. By the way: GCC
is an excellent software, and I am aware of the complexities that you must
deal with to make it work for so many different platforms!

Yours truly,
Radek
_________________________________________________

Dr. Radek Pecher
RDR, Earth Sciences
University of Leeds
Leeds, LS2 9JT
United Kingdom
TEL: +44 (0) 113 233 1926
FAX: +44 (0) 113 245 6233


#include <stdlib.h>

#ifdef __TURBOC__
  #define _cdecl            // non-ANSI keyword
  #define __int64 long      // non-ANSI keyword (see 'winnt.h')
  #define __ptr64 long*     // accompanies '__int64'
  #include <stdcomp.h>      // Borland's compiler/system related directives
  #undef _RWSTD_LONG_LONG   // suppress the 64-bit integers
#endif

using namespace std;
#include <valarray>
#include <iostream>

template <class T> class CTensor1D : public valarray<T>
{
public:
  CTensor1D() { MemPtr = 0; }
  CTensor1D(const T& val, size_t s) : valarray<T>(val, s) { Offset(); }
  CTensor1D(const CTensor1D<T>& ar) : valarray<T>(ar) { Offset(); }
  CTensor1D<T>& operator=(const CTensor1D<T>& ar) { valarray<T>::resize(
    ar.size()); valarray<T>::operator=(ar); Offset(); return *this; }
  T  operator[](size_t ind) const { return MemPtr[ind]; }
  T& operator[](size_t ind) { return MemPtr[ind]; }
protected:
  void Offset() { MemPtr = &valarray<T>::operator[](0) - 1; }
  T* MemPtr;
};

template<class T> ostream& operator<<(ostream& out, const CTensor1D<T>& ar)
{
  out << "{";
  for (size_t i=1; i<ar.size(); i++)
    out << ar[i] << ",";
  out << ar[ar.size()] << "}";
  return out;
}

template <class T> class CTensor2D : public CTensor1D<CTensor1D<T> >
{
public:
  CTensor2D() { MemPtr = 0; }
  explicit CTensor2D(size_t rows, size_t cols, const T c = T()) :
    CTensor1D<CTensor1D<T> >(CTensor1D<T>(c, cols), rows) {}
  CTensor2D(const CTensor2D<T>& ar) : CTensor1D<CTensor1D<T> >(ar) {}
  void resize(size_t rows, size_t cols, const T c = T())
    { (*this) = CTensor2D<T>(rows, cols, c); }
};

template<class T> ostream& operator<<(ostream& out, const CTensor2D<T>& ar)
{
  out << "{";
  for (size_t i=1; i<ar.size(); i++)
    out << ar[i] << ", ";
  out << ar[ar.size()] << "}";
  return out;
}

int main()
{
  CTensor2D<int> matr1;
  CTensor2D<int> matr2(3, 2, 7);
  CTensor2D<int> matr3(matr2);
  matr2.resize(2, 3, 0);
  matr2[1][1] = 11; matr2[1][2] = 12; matr2[1][3] = 13;
  matr2[2][1] = 21; matr2[2][2] = 22; matr2[2][3] = 23;

  cout << matr1.size() << " " << matr2.size() << " " << matr3.size() << endl;
  cout << matr2 << endl;
  cout << matr3 << endl;

  return 0;
}

Matrix.out


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]