Seltsames Segmentation Fault



  • Hallo,

    ich komme einfach nicht dahinter, was die Ursache für diesen Fehler ist: In Zeile 19 (beim "delete [] a") bekomme ich immer den Fehler "Segmentation Fault".

    struct term {
    	unsigned int col;
    	unsigned int row;
    	signed int value;
    };
    
    void teilaufgabe3 () {
    	term* a = init ( 8, 6, 6 );
    
    	unsigned int pos = 0;
    	set ( a, ++pos, 0, 0, 15 );
    	set ( a, ++pos, 0, 3, 22 );
    	set ( a, ++pos, 0, 5, -15 );
    	set ( a, ++pos, 1, 1, 11 );
    	set ( a, ++pos, 1, 2, 3 );
    	set ( a, ++pos, 2, 3, -6 );
    	set ( a, ++pos, 4, 0, 91 );
    	set ( a, ++pos, 5, 2, 28 );
    
    	term* b = init ( 0 );
    	transpose ( a, b );
    
    	// [...]
    
    	delete [] a;
    	delete [] b;
    }
    

    Die verwendeten Funktionen sind so definiert:

    void set ( term* a, const unsigned int pos, const unsigned int col, const unsigned int row, const int value ) {
    	a[pos].col = col;
    	a[pos].row = row;
    	a[pos].value = value;
    }
    
    term* init ( const unsigned int len, const unsigned int x = 0, const unsigned int y = 0 ) {
    	term* a = new term [ len + 1 ];
    	if ( x != 0 && y != 0 ) {
    		set ( a, 0, x, y, len );
    	}
    	return a;
    }
    
    void transpose ( const term* const a, term* b ) {
    	for ( unsigned int i = 0; i <= static_cast<unsigned>( a[0].value ); i++ ) {
    		b[i].col = a[i].row;
    		b[i].row = a[i].col;
    		b[i].value = a[i].value;
    	}
    }
    

    Kommentiere ich aber Zeile 17 aus ("b[i].col = a[i].row"), so läuft "teilaufgabe3()" problemlos durch.

    Weiß jemand Rat? 😕



  • Du zerschießt dir den Heap in transpose. b hat nur ein Element (b[0]), du greifst aber auf b[1], b[2] usw. zu.



  • Danke, ich muss blind gewesen sein xD



  • [klugscheisser]delete [] a; versteht mein C Compiler nicht 😞 [/klugscheisser] 😉


Anmelden zum Antworten