Matrixmultiplikation mit std::array



  • Hallo zusammen,

    ich experimentiere gerade mit einer selbst geschriebenen Matrixklasse und wollte zu Testzwecken auch die Matrixmultiplikation mit den Containern der Standardbibliothek (std::vector und std::array) testen. Mit meiner eigenen Klasse und std::vector gibt es keine Probleme, wohl aber mit std::array:

    Minimalbeispiel:

    #include <iostream>
    #include <random>
    #include <array>
    #include <ctime>
    
    using namespace std;
    
    array<array<int,1000>,1000> mat_mul(const array<array<int,1000>,1000>& lhs,
    		const array<array<int,1000>,1000>& rhs) {
    
    	array<array<int,1000>,1000> res{};
    
    	for(int i=0;i<1000;++i) {
    		for(int j=0;j<1000;++j) {
    			res[i][j] = 0; // should be redundant
    			for(int k=0;k<1000;++k) {
    				res[i][j] += lhs[i][k] * rhs[k][j];
    			}
    		}
    	}
    	return res;
    }
    
    int main() {
    	default_random_engine engine{(long unsigned int)time(0)};
    	uniform_int_distribution<int> w{1,10};
    
    	array<array<int,1000>,1000> lhs{}, rhs{};
    	for(int i=0;i<1000;++i) {
    		for(int j=0;j<1000;++j) {
    			lhs[i][j] = w(engine);
    			rhs[i][j] = w(engine);
    		}
    	}
    
    	array<array<int,1000>,1000> res = mat_mul(lhs, rhs);
    }
    

    Der compiler meckert nicht, das Programm endet aber mit einem seg-fault.
    Hat jemand eine Ahnung, was hier schief laueft?

    Viele Gruesse,
    Urwald



  • @Urwald sagte in Matrixmultiplikation mit std::array:

    Hat jemand eine Ahnung, was hier schief laueft?

    Das Array wird wohl nicht auf den Stack passen.



  • Hmm, das war tatsaechlich das Problem. Vielen Dank fuer deine Hilfe!


Anmelden zum Antworten