0

割り当てられたメモリは、OS セッションの開始以降のガベージ値を保持していますか? プログラムの実行時セッションでガベージ値として名前を付ける前に、何らかの意味がありますか? もしそうなら、なぜですか?

Linux カーネル プログラミング、デバイス ドライバ プログラミングに関する学習資料についてアドバイスが必要です。また、コンピュータ デバイスが実際にどのように動作するかについて理解を深めたいと考えています。「ガベージ バリュー」のような状況に陥ってしまい、プログラミング言語をよりよく理解するために、別のことも勉強しなければならないと感じています。私は独学で勉強していて、混乱する状況がたくさんあります。どんなアドバイスも本当に役に立ちます。

4

5 に答える 5

4

"Garbage value" is a slang term, meaning "I don't know what value is there, or why, and for that reason I will not use the value". It is "garbage" in the sense of "useless nonsense", and sometimes it is also "garbage" in the sense of "somebody else's leavings".

Formally, uninitialized memory in C takes "indeterminate values". This might be some special value written there by the C implementation, or it might be something "left over" by an earlier user of the same memory. So for examples:

  • A debug version of the C runtime might fill newly-allocated memory with an eye-catcher value, so that if you see it in the debugger when you were expecting your own stored data, you can reasonably conclude that either you forgot to initialize it or you're looking in the wrong place.
  • The kernel of a "proper" operating system will overwrite memory when it is first assigned to a process, to avoid one process seeing data that "belongs" to another process and that for security reasons should not leak across process boundaries. Typically it will overwrite it with some known value, like 0.
  • If you malloc memory, write something in it, then free it and malloc some more memory, you might get the same memory again with its previous contents largely intact. But formally your newly-allocated buffer is still "uninitialized" even though it happens to have the same contents as when you freed it, because formally it's a brand new array of characters that just so happens to have the same address as the old one.

One reason not to use an "indeterminate value" in C is that the standard permits it to be a "trap representation". Some machines notice when you load certain impossible values of certain types into a register, and you'd get a hardware fault. So if the memory was previously used for, say, an int, but then that value is read as a float, who is to say whether the left-over bit pattern represents a so-called "signalling NaN", that would halt the program? The same could happen if you read a value as a pointer and it's mis-aligned for the type. Even integer types are permitted to have "parity bits", meaning that reading garbage values as int could have undefined behavior. In practice, I don't think any implementation actually does have trap representations of int, and I doubt that any will check for mis-aligned pointers if you just read the pointer value -- although they might if you dereference it. But C programmers are nothing if not cautious.

于 2012-08-24T10:25:15.500 に答える
2

ガベージバリューとは?

メモリの場所で値に遭遇し、これらの値がどうあるべきかを決定的に言うことができない場合、それらの値はあなたにとってごみの値です。すなわち:値は不確定です。
最も一般的には、変数を使用して初期化しない場合、変数の値は不確定であり、ガベージ値を持っていると言われます。Uninitialized変数を使用すると、Undefined Behaviorが発生することに注意してください。これは、プログラムが有効なC / C ++プログラムではなく、(文字通り)任意の動作を示す可能性があることを意味します。

その場所に特定の値が存在するのはなぜですか?

今日のほとんどのオペレーティングシステムは、仮想メモリの概念を使用しています。ユーザープログラムに表示されるメモリアドレスは仮想メモリアドレスであり、物理アドレスではありません。仮想メモリの実装は、仮想アドレス空間をページ、連続する仮想メモリアドレスのブロックに分割します。使用が完了すると、これらのページは通常少なくとも4キロバイトになります。これらのページはコンテンツが明示的に消去されないため、再利用できるようにマークされているだけなので、適切に初期化されていない場合でも古いコンテンツが含まれています。

于 2012-08-24T09:58:39.317 に答える
0

一般的なOSでは、ユーザースペースアプリケーションは一定範囲の仮想メモリしか認識しません。この仮想メモリを実際の物理メモリにマップするのはカーネル次第です。

プロセスが(仮想)メモリの一部を要求すると、最初はその中に残っているものをすべて保持します。これは、プロセスの別の部分が以前に使用していた再利用されたメモリの場合もあれば、まったく異なるメモリの場合もあります。プロセスは使用されていました...または、まったく触れられておらず、マシンの電源を入れたときの状態になっている可能性があります。

通常、意味がないため、誰もあなたに代わってゼロ(または他の同様に任意の値)でメモリページをワイプすることはありません。メモリを好きなように使用するかどうかは完全にアプリケーション次第です。とにかくメモリに書き込む場合は、以前に何が入っていたかは関係ありません。

したがって、Cでは、未定義動作の苦痛の下で、変数に書き込む前に変数を読み取ることは許可されていません。

于 2012-08-24T09:58:39.630 に答える
0

特定の値に初期化せずに変数を宣言すると、そのメモリの一部を解放した別のプログラムによって以前に割り当てられた値が含まれている可能性があります。または、コンピューターが起動されたときのランダムな値である可能性があります ( iirc によると、初期バージョンの DOS では必要だったため、PC は起動時にすべての RAM を 0 に初期化していましたが、新しいコンピューターではこれを行わなくなりました)。たとえば、値がゼロになると想定することはできません。

于 2012-08-24T10:01:31.670 に答える
0

たとえば C のガベージ値は、通常、メモリを予約するだけで初期化しない場合、まだ初期化されていないため、ランダムな値を保持するという事実を指します (C は自動的にそれを行いません。 C はオーバーヘッドをできるだけ少なくするように設計されています)。メモリ内のランダムな値は、以前にそこにあったものの残り物です。

これらの以前の値はそこに残されます。通常、メモリをゼロまたはその他の値に設定することはあまり使用されず、後で再び上書きされるからです。一般的なケースでは、初期化されていないメモリを読み取ることには意味がありません (たとえば、考えられるセキュリティの問題を利用したい場合を除きます - メモリが実際にゼロになる特殊なケースを参照してください: Kernel zeroes memory? )。

于 2012-08-24T10:05:25.473 に答える