简体   繁体   中英

Accessing Matrices from Vector of Pointers Passed to Function in C++

I started writing in C++ a few months ago and am having some trouble using pointers. In the code that I've been writing for work, I've defined several matrices using the matrix_t class that I wrote. I created a vector that contains pointers to these matrices. I pass the vector to a function which accesses the matrices. Here's a simple example of what I'm trying to do:

#include <iostream>

#include "matrix_t.h"

using namespace std;

int mat_access (vector <matrix_t<int>*> &pmatrices)
{
    matrix_t<int> mat = *pmatrices[0];
    return mat.get_cell(0, 0);
    /*get_cell(r, c) returns the value at row r, column c*/
}

int main()
{
    vector <matrix_t<int>*> pmatrices(1);

    matrix_t <int> mat (1, 1, 1);
    /*mat_1(v, r, c) has r rows, c columns, and is filled with the value v*/
    pmatrices[0] = &mat;

    for (size_t i = 0; i < 5; i ++)
    {
        int k = mat_access(pmatrices);
        cout << k;
    }
}

If I step through the function while debugging, it works the first time the function mat_access is called. However, the second time that get_cell is called (even though its for the same row and column of the same matrix) a breakpoint is triggered and I get an error message in the output that says an invalid address is being accessed. I'm using App Verifier and the call stack location it returns has no source code. If I don't pass the vector of pointers to a function, but instead just access it directly in the main method, it works fine. Also, If I run the same code using vectors instead of matrices, it works fine. I don't think it's a problem with get_cell because I've used it at several other places in my code and I'm sure the indexing is correct. Here's what it looks like within the matrix_t class:

T get_cell (size_t row, size_t col) const
{   try
    {   
        if (row >= n_rows || col >= n_cols)
            throw myerror("Index out of bounds");
    }
    catch (exception &e)
    {
        cout << "Exception: " << e.what() << '\n';
    }
    return t_array [col * n_rows + row];
}

t_array contains the matrix elements and n_rows is the number of rows of the matrix. If all else fails, I'll just combine my matrices into one matrix and pass that to the function, but I'd like to understand why this isn't working. Any input would be appreciated.

Syntactically, I don't see any issues with your code. Try running this snippet (equivalent to what you have above) and see if you encounter the same error. If not, most likely the issue is in your matrix code.

int foo(vector<int*> &v) {
    int x = *v[0];
    return x;
}

int main() {
    vector<int*> v(1);
    int x = 9;
    v[0] = &x;
    for(int i = 0; i < 5; ++i) {
        int y = foo(v);
        cout << y << endl;
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM