1

For the actual question, jump to question part. For an interesting real-world example of undefined behavior, keep reading :)

There was this enumeration:

struct EnumStruct 
{
   enum Enum
   {
       val0 = 0,
       val1,
       val2,
       val3,
       val4
   };
};

and in some function we had this:

const int arrayCount = 6;
int arr[] = {
        EnumStruct::val0,
        EnumStruct::val1,
        EnumStruct::val2,
        EnumStruct::val3,
        EnumStruct::val4
        InvalidValue
      };    

Then there was a loop that put arrayCount elements of the arr into a file. This was the Prepare() routine for the unit tests. And the unit test was supposed to check for the presence of the InvalidValue in the file. I was assigned a defect stating that the unit test fails. It worked perfect on my machine though. After a couple of hours of debugging I noticed that InvalidValue is actually #defined as -1, and there is a missing comma after val4. You can only imagine the swearwords that came out of my mouth in the address of whoever wrote that code (which worked perfectly for more than 3 years, actually).

Now, as you can see, the array is actually comprised of 5 values - 0, 1, 2, 3, 3 but the loop writes to the file the 6th element as well, which is of course Undefined Behavior. Now, while technically it is undefined, on Windows with MSVC no crashes happen - it just writes the garbage that is at that memory location. The thing is that if the garbage happens to be anything but 0, 1, 2, 3, or 4, the unit tests will succeed.

Question: It appears that the .vcproj file of the UT's is somehow patched before the UT's are built. I don't know how they do it, but with their build out-of-bounds array elements are always 0. It seems to me that the whole virtual memory is set to 0 before program execution. What project setting is that? Or am I imagining things? I mean, if it was just luck that there was a 0 sitting out-of-bound of an array, then upon multiple executions my luck would fail, wouldn't it? But it is always 0... I am confused. By the way, when I build the same project the out-of-bounds element always has different values on each execution. Can you please explain this? Thanks.

4

2 に答える 2

0

実際の質問に対して、最初はメモリは常に 0 ですか? まあ、依存するかもしれません。一般に、OS がメモリのページを提供すると、それはクリアされます (セキュリティ対策として、他のプロセスがメモリに持っていた値を読み取れないようにするため)。メモリが独自のプロセスで再利用されるまで、0 のように見えます。そこでは、以前に書いたものがすべて取得されます。

それに影響を与える可能性のあるコンパイラフラグもいくつかあります。初期化されていないメモリの問題を検出するために、デバッグ ビルドは、OS から割り当てられた後、プログラムに処理される前にパターンをメモリに書き込み、プログラムでメモリを解放した後、再割り当てする前に別のパターンを書き込むことがあります (解放されたメモリへのアクセスを検出するため)。何が起こったのかを簡単に特定できることを確認してください (デバッガーで値が0xDEADBEEFである場合、プログラムによってメモリが既に解放されていることがわかります。より正確な情報については、コンパイラ/IDE のドキュメントを読む必要があります。

于 2011-07-05T14:32:03.307 に答える
0

おっしゃる通り未定義なので、実装者は自由に好きなようにできます。私は Visual C++ についてまったく話すことができませんが、デバッグ ビルドを実行するときにメモリをゼロにするなどのことを行う他の製品を知っています。マイクロソフトが似たようなことをしている可能性があります。

于 2011-07-05T14:25:28.087 に答える