home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 18122 < prev    next >
Encoding:
Text File  |  1992-12-17  |  3.1 KB  |  105 lines

  1. Path: sparky!uunet!mcsun!julienas!sophia!hawai.inria.fr!gchow
  2. From: gchow@hawai.inria.fr (Gloria Chow)
  3. Newsgroups: comp.lang.c++
  4. Subject: Matrix Addition  Was Re: Complex Addition.
  5. Keywords: operator, memory leak
  6. Message-ID: <36223@sophia.inria.fr>
  7. Date: 13 Dec 92 16:33:47 GMT
  8. References: <1992Dec11.170849.18525@rs6000.bham.ac.uk> <1992Dec11.232954.7071@borland.com>
  9. Sender: news@sophia.inria.fr
  10. Organization: INRIA, Sophia-Antipolis (Fr)
  11. Lines: 92
  12.  
  13. In article <1992Dec11.232954.7071@borland.com>, pete@borland.com (Pete Becker) writes:
  14. |> In article <1992Dec11.170849.18525@rs6000.bham.ac.uk> pickerig@eee.bham.ac.uk (Guy Pickering) writes:
  15. |> >In ARM, the code for the `+` operator for the complex class is given as:
  16. |> >
  17. |> >inline complex operator+(complex a, complex b)
  18. |> >{
  19. |> >  return complex(a.re+b.re,a.im+b.im)
  20. |> >}
  21. |> >
  22. |> >In general, can I create an instance of a class in a member function and
  23. |> >return the object? Doesn't the complex object go out of scope?
  24. |> >
  25. |> 
  26. |>     Yes, it does.  But that's not the end of the inquiry.
  27. |> 
  28. |>     int add( int a, int b )
  29. |>     {
  30. |>     return a+b;
  31. |>     }
  32. |> 
  33. |>     Here, too, the temporary goes out of scope, but the compiler knows how
  34. |> to deal with that.  Same thing for an object of a user-defined type.
  35. |> 
  36.  
  37. How about if the structure is a bit more complex, especially one with
  38. dynamically allocated memory.  I tried the following (I appologize if it's
  39. syntactically incorrect, I no longer have the version which I actually tried.
  40. This one is just from memory):
  41.  
  42. class MATRIX
  43. {
  44. protected:
  45.     double      **entry;
  46.     int         nrows, ncols;
  47. public:
  48.     MATRIX( int num_rows, int num_cols );
  49.     MATRIX& operator=( const MATRIX& equivalent );
  50.     MATRIX& operator+( const MATRIX& addend );
  51. };
  52.  
  53. MATRIX::MATRIX(int num_rows, int num_cols )
  54. {
  55. register int    i;
  56.  
  57.     nrows = num_rows;
  58.     ncols = num_cols;
  59.  
  60.     entry = new double *[ nrows ];
  61.     for( i=0 ; i<nrows; i++ )
  62.         entry[i] = new double[ ncols ];
  63. }
  64.  
  65. MATRIX& MATRIX::operator=( const MATRIX& equivalent )
  66. {
  67. register int    i;
  68.  
  69.     for( i=0; i<nrows; i++)
  70.         memcpy( (void*)entry[i], 
  71.                 (void*)equivalent.entry[i],
  72.                 sizeof( double ) * ncols);
  73.  
  74.     return *this;
  75. }
  76.  
  77. MATRIX& MATRIX::operator+( const MATRIX& addend )
  78. {
  79. register int    i,j;
  80. MATRIX          sum(nrows, ncols);
  81.  
  82.     for( i=0 ; i<nrows ; i++ )
  83.         for ( j=0 ; j<ncols ; j++ )
  84.             sum.entry[i][j] = entry[i][j] + addend.entry[i][j];
  85.  
  86.     return sum;
  87. }
  88.  
  89.  
  90. Okay, the problem is when I tried to use the "+" operator as follows:
  91.  
  92.     matrix_c = matrix_a + matrix_b;
  93.  
  94. the "+" operation works and sum contains the proper matrix, that is until
  95. "+" finishes and goes out of scope.  The "return" statement makes a temporary
  96. copy of sum (ie. the values of its nrows, ncols, and the pointer entry), and
  97. then proceed to destroy sum automatically.  So, now the entry pointer of this
  98. temporary is no longer pointing to a valid block of memory!!   
  99.  
  100. Is this really what happened?  I am new to C++, I am not sure my interpretation
  101. is correct.  But all I can tell is that this simple addition and assignment 
  102. sequence DOESN'T work, and I don't know how to get around it.  HELP!!
  103.  
  104. gloria
  105.