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]

no default constructor for double



Hi,

I'm running the version of egcs packaged with RH5.2 x86 (sorry, I'm not
infront of my home system I can't get teh version number from here).  I
have attached a copy of a template class called "rarray" that is a
resizeable array not unlike vector.  When I instantiated a rarray<double>
and resized it (by calling reserve()), the values between the
indices _array_size and new_size are garbage values.  I thought that the
c++ standard mandated that the default constructors for all the numeric
types by 0 (or 0.0 in the case of double).  Assuming that this is true,
shouldn't

double *double_array = new [100] double();

be initialized with 100 0.0 values?

thank you in advance.

Daniel

Daniel Supernaw-Issen 
danielsi@io.com
#ifndef RARRAY_H
#define RARRAY_H

#ifdef _MSC_VER
#include <crtdbg.h>
#define assert _ASSERT
#else
#include <assert.h>
#endif

template <class T>
class rarray
{
protected:
  long int _array_size;
  T* _array;
public:
  rarray(long int reserve_size = 0) : _array_size(reserve_size)
  {
	_array = (reserve_size ? (new T [reserve_size]()) : 0);
  };

  rarray(const rarray<T>& rhs)
  {
	_array_size = rhs._array_size;
	_array = new T[_array_size]();
	long int index;
	for(index = 0; index < _array_size; index++)
	{
	  _array[index] = rhs._array[index];
	}
  };

  rarray<T>& operator=(const rarray<T>& rhs)
  {
	if(&rhs != this)
	{
	  delete [] _array;
	  _array_size = rhs._array_size;
	  _array = new T[_array_size]();
	  long int index;
	  for(index = 0; index < rhs._array_size; index++)
	  {
		_array[index] = rhs._array[index];
	  }
	}
	return *this;
  };

  operator const T* () const { return _array; };
  operator T* () { return _array; };
  operator void* () { return _array; };
  
  ~rarray()
  {
	if(_array)
	{
	  delete [] _array;
	}
	_array = 0;
  };

  long int size() const { return _array_size; };
  
  void reserve(long int reserve_size)
  {
	if(reserve_size > _array_size)
	{
	  long int new_size = (2 *(reserve_size + 1));
	  T* new_array = new T[new_size]();
	  long int index;
	  for(index = 0; index < _array_size; index++)
	  {
		new_array[index] = _array[index];
	  }
	  delete [] _array;
	  _array = new_array;
	  _array_size = new_size;
	}
  };

  T& operator[](long int index)
  {
	assert(index < _array_size);
	return *(_array + index);
  };

  const T& operator[](long int index) const
  {
	assert(index < _array_size);
	return *(_array + index);
  };

  T& at(long int index)
  {
	assert(index < _array_size);
	return *(_array + index);
  };

  const T& at(long int index) const
  {
	assert(index < _array_size);
	return *(_array + index);
  };

  T* offset_address(long int index)
  {
	assert(index < _array_size);
	return (_array + index);
  };

  void assert_index(long int index) const
  {
	assert(index < _array_size);
  }

};

#endif

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