Help with returning pointer (to vector) from function

I'm trying to create a function that transposes a matrix in the form of a vector array. The program almost works, except that the first 2 values of the new vector returns these large numbers instead of the values given to them. It seems to be a problem with the returning process, as when I outputted the values in the function everything was fine.

Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <vector>
using namespace std;

vector<int> * matrixTransposer(vector<int> * matrixPointer, int r, int c);

int main()
{
    vector<int> matrix;     //matrix
    int r, c;               //rows & columns

    //getting rows and columns from user
    cout << "Input rows: ";
    cin >> r;
    cout << "Input columns: ";
    cin >> c;

    //resize matrix to fill in total data of matrix

    matrix.resize(r*c);

    //filling matrix with identifying digits

    for (int i = 0; i < r*c; i++)
        matrix[i] = i % c;

    //output matrix data
    cout << endl << "Matrix: " << endl << endl;
    int counter = 0;
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
        {
            cout << matrix[counter];
            counter++;
        }
        cout << endl;
    }
    cout << endl;

    //setting pointer for matrix
    vector<int> * matrixPointer;
    matrixPointer = &matrix;

    //creating matrix transpose variable
    vector <int> matrixTranspose;
    matrixTranspose.resize(r*c);

    //giving matrix transpose its value
    matrixPointer = matrixTransposer(matrixPointer, r, c);
    matrixTranspose = *matrixPointer;

    //switching r value with c value
    int temp = c;
    c = r;
    r = temp;

    //output matrix transpose
    cout << endl << "Transposed Matrix: \n\n";
    counter = 0;
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
        {
            cout << matrixTranspose[counter] << " ";
            counter++;
        }
        cout << endl;
    }

    return 0;
}

vector<int> * matrixTransposer(vector<int> * matrixPointer, int r, int c)
{
    //copying initial matrix
    vector <int> matrix;
    matrix.resize(r*c);

    matrix = *matrixPointer;

    //creating transpose matrix
    vector <int> matrixTranspose;
    matrixTranspose.resize(r*c);

    //outputing matrix for debugging
    cout << "Matrix Value: ";
    for (int i = 0; i < r*c; i++)
    {
        cout << matrix[i] << " ";
    }
    cout << endl;

    //setting value for transposing matrix
    cout << "Matrix Transpose step-by-step: ";
    int counter = 0;
    for (int i = 0; i < c; i++)
    {
        for (int j = 0; j < r; j++)
        {
            matrixTranspose[counter] = matrix[j*c + i];
            cout << matrixTranspose[counter] << " ";
            counter++;
        }
    }
    cout << endl;

    //outputing matrix tranposed for debugging
    cout << "Matrix Transpose end value: ";
    for (int i = 0; i < r*c; i++)
    {
        cout << matrixTranspose[i] << " ";
    }
    cout << endl;

    //setting pointer value;
    matrixPointer = &matrixTranspose;

    //returning pointer
    return matrixPointer;
}


And its output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Input rows: 3
Input columns: 5

Matrix:

01234
01234
01234

Matrix Value: 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
Matrix Transpose step-by-step: 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4
Matrix Transpose end value: 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4

Transposed Matrix:

5659016 5640704 0
1 1 1
2 2 2
3 3 3
4 4 4
Last edited on
The matrix vector that you create in the matrixTransposer will go out of scope when the function ends so what will be returned is a pointer to a non-existing vector. It's better to simply return the vector by value. You don't need to worry about this being inefficient. Returning a local variable will be optimized so that it doesn't need to do a full copy of the vector when you return it.

1
2
3
4
5
vector<int> matrixTransposer(vector<int> * matrixPointer, int r, int c)
{
	...
	return matrix;
}


There is no point in creating a vector and then resizing it if you are going to assign (copy) another vector to it in the line below. Just initialize the vector with the return value of the function directly.

1
2
3
4
5
6
int main()
{
	...
	vector<int> matrixTranspose = matrixTransposer(&matrix, r, c);
	...
}


You don't really need to use a pointer in the function at all. The vector that the pointer points to will always be copied anyway so why not simply make the matrix variable the parameter and have it copied directly. This is also better because it prevents anyone from passing a null pointer to the function (it also avoids copying in case you pass an rvalue-reference to the function).

 
vector<int> matrixTransposer(vector<int> matrix, int r, int c);

If you make this change the vector is passed as-is, without using a pointer.

 
vector<int> matrixTranspose = matrixTransposer(matrix, r, c);
Last edited on
Damn, that's eye opening. Cheers mate. I always thought you needed pointers to pass arrays and vectors between functions.
Topic archived. No new replies allowed.