ページ 11

ポインタ

Posted: 2015年2月06日(金) 22:16
by cc

コード:

DWORD MyAddr = 0x12345678;
*(DWORD *)MyAddr = 0x02;
すいません、これはどういう意味ですか?

Re: ポインタ

Posted: 2015年2月06日(金) 23:02
by みけCAT
言語が明示されていませんが、コードタグの表示よりC++と仮定します。
DWORDは標準で定義されていないようですが、整数を格納する型を表す識別子であると仮定します。

1行目は、直感的に考えるとDWORD型の変数MyAddrを宣言し、0x12345678で初期化する、という意味になります。

N3337 5.2.10 Reinterpret castより引用
A value of integral type or enumeration type can be explicitly converted to a pointer. A pointer converted
to an integer of sufficient size (if any such exists on the implementation) and back to the same pointer type
will have its original value; mappings between pointers and integers are otherwise implementation-defined.
N3337 5.3.1 Unary operatorsより引用
The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an
object type, or a pointer to a function type and the result is an lvalue referring to the object or function
to which the expression points. If the type of the expression is “pointer to T,” the type of the result is “T.”
N3337 5.17 Assignment and compound assignment operatorsより引用
1. The assignment operator (=) and the compound assignment operators all group right-to-left. All require a
modifiable lvalue as their left operand and return an lvalue referring to the left operand. The result in all
cases is a bit-field if the left operand is a bit-field. In all cases, the assignment is sequenced after the value
computation of the right and left operands, and before the value computation of the assignment expression.
With respect to an indeterminately-sequenced function call, the operation of a compound assignment is
a single evaluation.
(中略)
2 In simple assignment (=), the value of the expression replaces that of the object referred to by the left
operand.
2行目は0x12345678を処理系で定義された写像でDWORD型のデータを指すポインタに変換したポインタが指すデータの値を0x02に書き換える、という意味になります。

Re: ポインタ

Posted: 2015年2月07日(土) 11:51
by cc
アセンブリ

コード:

mov eax,0x123456789
mov [eax],0x02
C++言語

コード:

DWORD MyAddr = 0x12345678;
*(DWORD *)MyAddr = 0x02;
って感じです。

Re: ポインタ

Posted: 2015年2月07日(土) 12:24
by みけCAT
cc さんが書きました:アセンブリ

コード:

mov eax,0x123456789
mov [eax],0x02
C++言語

コード:

DWORD MyAddr = 0x12345678;
*(DWORD *)MyAddr = 0x02;
って感じです。
本当ですか。それなら、ccさんの環境ではDWORD型の0x12345678は値0x123456789を持つポインタにマップされるようですね。
eaxの大きさが32ビットより大きいようなので、x86ではなさそうですね。(となると、movの意味も考え直す必要がありそうですが)
そもそも、アセンブリの2行目でメモリのサイズが指定されていないので、やはりx86とは異なる、サイズの指定が必要ない環境なのですね。