2011-12-10 93 views
-4

我將BufferOut定義爲16元素數組。堆棧周圍變量損壞。爲什麼會發生?

但我在調試時遇到此消息。

Run-Time Check Failure #2 - Stack around the variable 'BufferOut' was corrupted.

HIDBufferOutchar陣列的16元件

HIDBufferOut BufferOut; // first element is the report ID 
    BufferOut[0] = 0; 
    BufferOut[1] = 211; 
    BufferOut[16] = 44; 
    BufferOut[4] = 4; 

    . 
    . 
    . 
    . 
    etc 
+2

錯誤消息足夠描述。如果你不能弄明白,那麼我們也不能沒有看到源代碼。 – ybungalobill

+0

'HIDBufferOut BufferOut; \t \t \t // \t第一元件是報告ID \t \t BUFFEROUT [0] = 0; \t \t BufferOut [1] = Marker(); \t \t BufferOut [16] =(int)44; \t \t BufferOut [4] = typ; ' –

+3

因爲你在數組中插入了第17個元素 –

回答

2

如果BufferOut是一個16元件緩衝區,然後有效索引是0,...,15。這意味着:

BufferOut[16] = (int) 44; 

寫出數組的邊界,導致內存損壞。

+0

我試圖用17個元素來定義它,問題仍然相同 –

+1

你在哪裏創建你的數組?我在上面的代碼中看不到它。 – Bart

+2

@ user1091216:如果是這樣,那麼你仍然沒有提供足夠的信息。 – ybungalobill

0

當你寫上已經超出了你的範圍造成緩衝區溢出的位置,並從這段代碼的堆棧腐敗清楚這通常發生: BufferOut [16] =(int)44; //您在第17個位置寫出界限

+0

將數組大小更改爲17個元素,如果它仍然存在,請執行清理工作,而不是從新建立項目。 –