INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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:    3
Number of columns: 2

CASE 1
INPUT 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 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) = 1
mat(0,1) = 2
mat(1,0) = 3
mat(1,1) = 4
mat(2,0) = 5
mat(2,1) = 6

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.

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

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;
...
} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close