1

非常に単純なコードがあり、ファイルから文字を読み取ります。インデックスyが低から高に繰り返される場合、すべてが機能します。しかし、高から低 (コメント行) に繰り返すと、セグ フォールトの問題が発生します。誰かがなぜこれが起こるのか説明できますか? ありがとうございました!

void read_ct_from_file( unsigned char** ct, const size_t row, const size_t column, FILE* inf ) {
    size_t x, y;
    for( x = 0; x < row; x++ ) {
        for( y = 0; y < column; y++ ) { 
        //for( y = column - 1; y >= 0; y-- ) { // iterate from high to low

              fscanf( inf, "%02x", &ct[x][y] );
              printf( "%02x ", ct[x][y] );
        }
        printf( "\n" );
    }
}
4

3 に答える 3

3

size_tは署名されていないため、ループはwhich is >= 0 でy = 0続行されます。max_unsigned

于 2012-06-28T15:53:57.217 に答える
0

ところで、unsigned size_tインデックスを持ち、ラップアラウンドアンダーフローを回避するための良い方法は次のとおりです。

void read_ct_from_file( unsigned char** ct, const size_t row, const size_t column, FILE* inf ) {
    size_t x, y;
    for( x = 0; x < row; x++ ) {
        for ( y = column; y-- > 0; ) { // iterate from high to low

              fscanf( inf, "%02x", &ct[x][y] );
              printf( "%02x ", ct[x][y] );
        }
        printf( "\n" );
    }
}
于 2012-07-04T10:50:03.443 に答える
-1
for( y = column - 1; y > 0; y-- ) { // iterate from high to low

これで試してください。

于 2012-06-28T15:57:41.350 に答える