I am using VS08 to create / run the following C ++ code:
Code> #include & lt; Stdlib.h & gt; Straight header {int calculation; } * LstHeader = Zero; Int main () {for (int i = 0; i & lt; 10; i ++) {lstHeader = (header *) realloc (lstHeader, sizeof (header) + i); LstHeader [i] .count = I; } Return 1; }And after running, I'm following VS The exception is:
Windows has introduced a breakpoint in MyProgram.exe. This may be due to the corruption of the stack, which indicates any bugs loaded in MyProgram.exe or any DLL. This user can also be pressed by F12 when there is a focus on MyProgram.exe. There may be more diagnostic information in the output window
I have tried malloc lstHeader instead of specifying null, but the same VS An exception occurred and I can not understand y
Do you think that when
i meets 1 There is a need to think, think. Let's assume a four-byte integer. On
i == 0 , you assign enough bytes to an extra zero bytes in addition to a
header . You can then set the
header from [0] .count to 0. There is no problem, you have allocated only four bytes. On
i == 1 , you assign enough bytes for more than one
header (for a total of five bytes). You can then set the
header [1] .count <1>. It changes your byte 4, 5, 6 and 7 (zero-based), in spite of you only have 0 to 4 bytes available. In other words, for that operation, you need additional four bytes for the one extra byte.
See it graphically: <+++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++ | | | | | | | | | | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 2 | 2 | 2 | Offset: | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | 1 | 2 | 3 | + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + = 0, get: & lt; ------- & gt; Use: & lt; ------- & gt; I = 1, get: & lt; ---------> Use: & lt; --------------- & gt; I = 2, get: & lt; ----------- & gt; Use: & lt; ----------------------- & gt; I = 3, get: & lt; ------------- & gt; Use: & lt; ------------------------------- & gt; I get = 4, get: & lt; --------------- & gt; Use: & lt; --------------------------------------- & gt; I = 5, get: & lt; ----------------- & gt; Use: & lt; ---------------------------------------------- - & gt;
As you can see, each walk gives you an additional byte, but you need an additional four bytes. Now it can actually work for a while, because most code
Comments
Post a Comment