0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

組込みC > 症状: 構造体の隣の値まで取得する

Last updated at Posted at 2017-09-19
動作環境
とある16bit組込み開発環境
sprintf関数は低メモリ消費版ライブラリ使用

問題のコード

問題のあった状況を抽出すると以下のようなコードになる。

# include <stdio.h>
# include <stdbool.h>

typedef struct {
	float pi;
	bool yesno;
} MY_DATA;

int main(void) {
	MY_DATA adata;

	adata.pi = 0.0;
	adata.yesno = false;

	printf("1:%d\n", adata.yesno);
	printf("2:%Ld\n", adata.yesno);

	adata.pi = 3.141592;
	adata.yesno = false;

	printf("3:%d\n", adata.yesno);
	printf("4:%Ld\n", adata.yesno);
	
	return 0;
}

上記において、4番の値が0でない。
(例: 655360)

問題発生条件

以下の条件が全て成立する時だけ発生する。

  1. 某開発環境のprintf関数使用時
    • gccなどにあるような一般的なprintf関数ではない
  2. printf書式指定子の指定間違い
    • "%d"とするところを"%Ld"としていた
  3. 構造体内の隣の変数piの値が0以外の時

起きたことの推測

構造体変数のメモリ配置は以下に例がある。
https://stackoverflow.com/questions/20737176/how-are-struct-members-allocated-in-memory

上記の構造体ではpiの後にyesnoが続いている。

| pi | yesno |

yesnoの値を問題のprintf()で"%Ld"として出力しようとした時に、piの領域も含めて読んだ値が出力され、655360のような値が出てきたように思われる。

"%d"に修正したところ、0が出るようになった。

予防措置

  1. printf()の書式指定子間違いがないことを確認する
  2. デバッグ時にyesno以外のメンバ変数にダミー値を入れて、yesno値に影響がないことを確認する
    • 変数全てに関して、同じ確認を行う
  3. データ測定時の動作検証をする

「データ測定」がいつでも可能という状況でない場合、措置1,2できちんと対処しておくことで、措置3の検証時間を抑える。

補足

以下のprintfでは発生しない。

  • ideone上のC (gcc version 6.3)
  • gcc (version 4.4.7) on CentOS 6.8

書式指定子間違いがあっても、隣の値を読まないよう処理がきちんと実装されているのかもしれない。

書式指定子の指定間違いをきちんと警告として出す開発環境ならこういうミスは減るだろう。
例として、@fujitanozomu さんに教えていただいた方法
https://ideone.com/RLp2fr

compilation info
prog.c: In function ‘main’:
prog.c:121:14: error: format ‘%Ld’ expects argument of type ‘long long int’, but argument 2 has type ‘int’ [-Werror=format=]
  printf("2:%Ld\n", adata.yesno);
              ^
prog.c:127:14: error: format ‘%Ld’ expects argument of type ‘long long int’, but argument 2 has type ‘int’ [-Werror=format=]
  printf("4:%Ld\n", adata.yesno);
              ^
cc1: some warnings being treated as errors

訂正

(追記 2017/09/20)

printf()でなくsprintf()を用いたchar 配列への代入で起きていた。
ideoneでsprintf()を試したが、同じ症状にはならない。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?