2013年12月8日日曜日

開発環境

C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅡ部(単純なプログラミング)の15章(デバッグと最適化)、15.6(最適化)、15.6.1(強力な2の累乗)、設問 15-1を解いてみる。

その他参考書籍

設問 15-1.

int型は32ビット、4バイト、char型は8ビット、1バイト、memset(s, c, n)は文字列(文字配列)の最初のn文字の中に文字cを入れ、sを返す関数。

-1(char型、0xff)の場合、memset関数でmatrix[i][j](int型)には0xffffffff(10進数で-1)がセットされる。よって上手くいく。

1(char型0x01)の場合、memset関数でmatrix[i][j](int型)には0x01010101(10進数で16^6 + 16^4 + 16 @ 1 = 16843009)がセットされる。よって上手くいかない。

確認。

コード

sample.c

#include <stdio.h>
#include <memory.h>

#define X_SIZE 60
#define Y_SIZE 32

int matrix1[X_SIZE][Y_SIZE];
int matrix2[X_SIZE][Y_SIZE];

int main()
{
    int i, j;
    memset(matrix1, -1, sizeof(matrix1));
    memset(matrix2, 1, sizeof(matrix2));
    for(i = 0; i < 5; i++){
        for(j = 0; j < 5; j ++){
            printf("%d, %d\n", matrix1[i][j], matrix2[i][j]);
        }
    }
    return (0);
}

makefile

CC=cc
CFLAGS=-g

sample: sample.c
 $(CC) $(CFLAGS) -o sample sample.c

clean:
 rm -f sample

入出力結果(Terminal)

$ ./sample
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
$

0 コメント:

コメントを投稿