Segmentation fault bei übergabe von Array



  • Hallo,

    ich experimentiere gerade ein wenig mit anjuta unter Linux rum. Ich habe folgendes kleines Programm geschrieben:

    [code]
    #include <stdio.h>

    void zeilemult(double matrix[1][1])
    {
    printf("%f\n%f\n%f" , matrix[1][1], matrix[0][0], matrix[1][0]);
    }

    int main(void)
    {
    double mat[1][1];
    mat[0][0] = 3.5;
    mat[1][0] = 0;
    mat[1][1] = 1,9;
    zeilemult(mat);
    return (0);
    }

    Führe ich das Programm aus bekomme ich einen segmentation fault. Der Wert für
    matrix[1][0] wird nicht ausgegben. Lasse ich die Zeile mat[1][0] = 1,9 weg funktioniert das Programm.

    Was mache ich falsch?

    Danke



  • double mat[1][1]; 
    mat[0][0] = 3.5; 
    mat[1][0] = 0; 
    mat[1][1] = 1,9;
    

    Viel zu wenig Speicher. Mach mal double mat[2][2];



  • deleteme.cpp(5) : warning C6385: Invalid data: accessing 'matrix[1]', the readable size is '8' bytes, but '16' bytes might be read: Lines: 5
    deleteme.cpp(13) : warning C6201: Index '1' is out of valid index range '0' to '0' for possibly stack allocated buffer 'mat'
    deleteme.cpp(14) : warning C6201: Index '1' is out of valid index range '0' to '0' for possibly stack allocated buffer 'mat'
    deleteme.cpp(13) : warning C6386: Buffer overrun: accessing 'mat', the writable size is '8' bytes, but '16' bytes might be written: Lines: 11, 12, 13
    deleteme.cpp(14) : warning C6386: Buffer overrun: accessing 'mat[1]', the writable size is '8' bytes, but '16' bytes might be written: Lines: 11, 12, 13, 14

    Ich würde mal vorschlagen, du liest das Kapitel über C-Arrays noch einmal durch.



  • double mat[2][2]; // !
    

    😉


Anmelden zum Antworten