Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic array assignments within function, with pointers returned to calling function

Status
Not open for further replies.

NickFort

Technical User
Jun 10, 2010
113
Hi all,

I'm very new to C++, having come from a Fortran 90+ and MATLAB background. I've been playing with pointers in C++, and I've stumbled upon something I can't quite explain. This is an arbitrary example I've come up with, just from messing around, trying to understand how C++ works.

In essence, what I'm trying to do is allocate a dynamic array within a function, populate the array, and return a pointer to the array in the calling function. If the function has a void return, and sends the pointer to the calling function by modifying an argument (i.e. a "subroutine" in Fortran terms), it doesn't work. If I do it as a function with a return (i.e. a "function" in Fortran terms), it works just fine. Let's call those Case 1 and Case 2, respectively.

I've put together a little example of what I mean:

Code:
#include <iostream>
using namespace std;

// Prototypes
void populate(int rows, int cols, double* ptr_to_mat);
double* populate_return(const int rows, const int cols);

// Main function
int main()
{
  
  int rows, cols;
  
  cout << "Number of rows:    ";
  cin >> rows;
  cout << "Number of columns: ";
  cin >> cols;
  cout << endl;

  double* ptr_to_mat;
  
  populate(rows, cols, ptr_to_mat);
  
  cout << endl << "OUTPUT IN main FROM populate" << endl;
  
  for (int r = 0; r <= rows-1; r++)
    for (int c = 0; c <= cols-1; c++)
      cout << "mat(" << r << "," << c << ") = " << ptr_to_mat[c+cols*r] << endl;
  
  cout << endl << endl;
  
  ptr_to_mat = populate_return(rows, cols);
  
  cout << endl << "OUTPUT IN main FROM populate_return" << endl;
  
  for (int r = 0; r <= rows-1; r++)
    for (int c = 0; c <= cols-1; c++)
      cout << "mat(" << r << "," << c << ") = " << ptr_to_mat[c+cols*r] << endl;
  
  
  delete [] ptr_to_mat;
  
  return 0;
}

// Other functions

// Return pointer by modifying argument -- CASE 1
void populate(const int rows, const int cols, double* ptr_to_mat)
{
  
  double* internal_mat = new double[rows*cols];
  
  cout << "INPUT IN populate" << endl;
  
  for (int r = 0; r <= rows - 1; r++)
    for (int c = 0; c <= cols - 1; c++)
      {
        cout << "mat(" << r << "," << c << ") = "; 
        cin >> internal_mat[c+cols*r];
      }

  ptr_to_mat = internal_mat;

  cout << endl << "OUTPUT IN populate" << endl;
  
  for (int r = 0; r <= rows-1; r++)
    for (int c = 0; c <= cols-1; c++)
      cout << "mat(" << r << "," << c << ") = " << ptr_to_mat[c+cols*r] << endl;

}

// Return pointer using "return" -- CASE 2
double* populate_return(const int rows, const int cols)
{
  
  double* internal_mat = new double[rows*cols];
  
  cout << "INPUT IN populate_return" << endl;
  
  for (int r = 0; r <= rows - 1; r++)
    for (int c = 0; c <= cols - 1; c++)
      {
        cout << "mat(" << r << "," << c << ") = "; 
        cin >> internal_mat[c+cols*r];
      }
  
  cout << endl << "OUTPUT IN populate_return" << endl;
  
  for (int r = 0; r <= rows-1; r++)
    for (int c = 0; c <= cols-1; c++)
      cout << "mat(" << r << "," << c << ") = " << internal_mat[c+cols*r] << endl;

  return internal_mat;
  
}

The result of running the above code is the following, where bold is the user input:

Code:
Number of rows:    [b]3[/b]
Number of columns: [b]2[/b]

CASE 1
INPUT IN populate
mat(0,0) = [b]1[/b]
mat(0,1) = [b]2[/b]
mat(1,0) = [b]3[/b]
mat(1,1) = [b]4[/b]
mat(2,0) = [b]5[/b]
mat(2,1) = [b]6[/b]

OUTPUT IN populate
mat(0,0) = 1
mat(0,1) = 2
mat(1,0) = 3
mat(1,1) = 4
mat(2,0) = 5
mat(2,1) = 6

OUTPUT IN main FROM populate
mat(0,0) = -1.72952e-41
mat(0,1) = -2.77962e-42
mat(1,0) = -2.77966e-42
mat(1,1) = -2.7797e-42
mat(2,0) = -6.02988e-42
mat(2,1) = -2.77979e-42


CASE 2
INPUT IN populate_return
mat(0,0) = [b]1[/b]
mat(0,1) = [b]2[/b]
mat(1,0) = [b]3[/b]
mat(1,1) = [b]4[/b]
mat(2,0) = [b]5[/b]
mat(2,1) = [b]6[/b]

OUTPUT IN populate_return
mat(0,0) = 1
mat(0,1) = 2
mat(1,0) = 3
mat(1,1) = 4
mat(2,0) = 5
mat(2,1) = 6

OUTPUT IN main FROM populate_return
mat(0,0) = 1
mat(0,1) = 2
mat(1,0) = 3
mat(1,1) = 4
mat(2,0) = 5
mat(2,1) = 6

My hypothesis is that it has to do with when the pointer is assigned in the calling function. So, in Case 2, it is assigned to the left-hand side before the information within the function is deallocated from memory, whereas in Case 1, that memory is deallocated before the pointer is assigned in the calling function, since the assignment happens on the right-hand side.

Does that sound plausible at all? If not, what's the reason Case 2 works, but Case 1 doesn't?

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
Case 2 works because it is a double* function and it is returning a double*.

Case 1 does not work because C uses call by value. This means that the value is copied to a temporary variable and that variable is used in the function. If the value is changed, it is not returned to the caller. If you wish to return a value, change it as follows
Code:
// Prototypes
void populate(int rows, int cols, double** ptr_to_mat);
...
int main()
{
...
  populate(rows, cols, &ptr_to_mat);
...
}
void populate(int rows, int cols, double** ptr_to_mat);
{
...
  *ptr_to_mat = internal_mat;
...
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top