From: suresh@papaya.iss.nus.sg (Suresh Thennarangam - Research Scholar)
Subject: Re: Can't I initialize a dynamically allocated object in its constructor
Originator: suresh@papaya
Organization: Institute of Systems Science, NUS, Singapore
Date: Tue, 22 Dec 1992 11:42:51 GMT

In article  <1992Dec18.161017.17058@news2.cis.umn.edu>,hansen@myria.cs.umn.edu (David Hansen)  writes:

In article <1992Dec17.205113.1180@IRO.UMontreal.CA>, laviers@IRO.UMontreal.CA (C
ocotte Minute) writes:
> |> Hello,
> |>
> |> I have the following problem.
> |>
> |>  I have created an class: Vector(k), it's
> |> a vector of k dimension (of doubles). After a whole week of debugging,
> |> I have come to the conclusion that you must'nt set the values of the element


:
:
:
> The corrected code appears below.  Enjoy!
:
:
> 
> 
> 
> 
> #include <stdlib.h>	// exit
> 
> //-------------------------------------------------------------------
> // Vector: vector of double
> //-------------------------------------------------------------------
> 
> class Vector{
> 
> double 	*item ;
> int 	size ;
> 
> 	
> public :
> 		
> // constructors 
> Vector() { item = new double[1] ; size = 1 ;}	// NOTE: use array form
> Vector(int array_size);
> Vector(const Vector & v);	// NOTE: new copy constructor
> 
> // destructors 
> ~Vector() { delete [] item ; }	// NOTE: use array form
> 
> // methods 
> int getsize() { return size ; }
> 
> void print(char *message);
> 
> 
> // operators 
> Vector operator+ (Vector& b);
> Vector& operator= (Vector& a);
> double& operator[] (int i);
> };
> 
> 
> // constructors
> 
> Vector::Vector(int array_size) { 
>    item = new double[array_size] ; 
>    size = array_size ;
>    printf("constructing\n");
>    for(int i=0;i<array_size;i++)item[i]=-1.0;
> }
> 
> // Definition of new copy constructor
> Vector::Vector(const Vector& v) {
>    int i;
>    size = v.size;
>    item = new double[size];
>    for (i=0; i<size; i++)
>       item[i] = v.item[i];
> }
> 
> 
> 
> 
> Vector& Vector::operator= (Vector& a){
>    if (getsize() != a.getsize()){
>       printf("operator '=':Vectors are not of the same size.\n");
>       exit(0);
>    }
>    for (int i = 0 ; i < a.getsize() ; i++){
>       item[i] = a.item[i] ;
>    }
>    return *this ;
> }
> 
> 
> double& Vector::operator[](int i){
>    return item[i] ;
> }
> 
> 
> void  Vector::print(char *message) { 
>    printf("%s\n",message);
>    for (int i = 0 ; i < size ; i++){
>       printf("       [%d]=%lf\n", i, item[i]);
>    }
> }
> 
> 
> 
> main(){
> 
> Vector v1(3),v2(3),v3(3),v4(3);
> 
> 
> 
> v1[0]=1.0;
> v1[1]=1.0;
> v1[2]=1.0;
> 
> v2[0]=2.0;
> v2[1]=2.0;
> v2[2]=2.0;
> 
> v3[0]=3.0;
> v3[1]=3.0;
> v3[2]=3.0;
> 
> v4=v1+v2+v3;
> 
> v4.print("v4");
> 
> }
> 
> 
> Vector Vector::operator+ (Vector& a){
>    if (getsize() != a.getsize()){
>       printf("operator '+':Vectors are not of the same size.\n");
>       exit(0);
>    }
>    Vector retVect(getsize()) ;
>    for (int i = 0 ; i < a.getsize() ; i++){
>       retVect.item[i] = item[i] + a.item[i] ;
>    }
>    return retVect ;
> }
> 
My apologies for including  the entire code above ....
Also to make things easier I have moved the function  
Vector Vector::operator+() to the bottom.

My point:

retVect used above is a automatic variable that will go out of scope
as soon as the function Vector::operator+() is exited and it's 
destructor will be called which will deallocate the memory
that was allocated by the constructor. Although the returned
Vector object is a copy and will have a valid item pointer, it will 
be pointing to deallocated data.


Nitpicking:

The operator= function should check for self-assignment
like so   " if ( this != &a) ". Saves a lot of work



      __                  
     (_   / /  o_   o  o |_
     __)/(_( __) (_(_ /_)| )_



***************************************************************************
* Suresh Thennarangam               *  EMail: suresh@iss.nus.sg(Internet) *
* Research Scholar                  *         ISSST@NUSVM.BITNET          *
* Institute Of Systems Science      *  Tel:  (065) 772 2588.              *
* National University Of Singapore  *  Facs.: (065) 778 2571          *
* Heng Mui Keng Terrace             *  Telex: ISSNUS RS 39988             *
* Singapore 0511.                   *                                     *
***************************************************************************