c++ - realloc failing to allocate -


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 calls to give you at least the resolution such as 16 bytes (1, 2, 3 or 16 and you get 16 Ask for 20 or 30, you get 32). But it is still undefined behavior and you should not trust it.

And, in any case, you will eventually reach the point where it will not help any more. The point is that where sizeof * (i + 1) is required, you go above the 16-byte threshold while sizeof + i that you want to stay below said. You will then write beyond your allocated memory (even with padding) and you will seize the memory area.

What you might want to do:

  lstHeader = reall (lstHeader, size (header) * (i + 1));   

will ensure that you have enough space for the required header elements in memory allocation because the reason is i + 1 Initial allocation should be an element, even if i is zero.

Comments