1

cf. Environment: Microsoft Visual Studio 2022 17.5

// main.c
int main() {
    int a = 0x12345, b = 0x67890;
    int arr[5] = { 0x1234, 0x2345, 0x3456, 0x4567, 0x5678 };
    return 0;
}

If I execute code above,
and when I check my memory allocated with debugger,
result is as follows.

0x0000002FC3EFF9F4  45 23 01 00  E#..
0x0000002FC3EFF9F8  cc cc cc cc  ????
0x0000002FC3EFF9FC  cc cc cc cc  ????
0x0000002FC3EFFA00  cc cc cc cc  ????
0x0000002FC3EFFA04  cc cc cc cc  ????
0x0000002FC3EFFA08  cc cc cc cc  ????
0x0000002FC3EFFA0C  cc cc cc cc  ????
0x0000002FC3EFFA10  cc cc cc cc  ????
0x0000002FC3EFFA14  90 78 06 00  ?x..
0x0000002FC3EFFA18  cc cc cc cc  ????
0x0000002FC3EFFA1C  cc cc cc cc  ????
0x0000002FC3EFFA20  cc cc cc cc  ????
0x0000002FC3EFFA24  cc cc cc cc  ????
0x0000002FC3EFFA28  cc cc cc cc  ????
0x0000002FC3EFFA2C  cc cc cc cc  ????
0x0000002FC3EFFA30  cc cc cc cc  ????
0x0000002FC3EFFA34  cc cc cc cc  ????
0x0000002FC3EFFA38  34 12 00 00  4...
0x0000002FC3EFFA3C  45 23 00 00  E#..
0x0000002FC3EFFA40  56 34 00 00  V4..
0x0000002FC3EFFA44  67 45 00 00  gE..
0x0000002FC3EFFA48  78 56 00 00  xV..

What I see is that memory is NOT allocated sequentially
and there exist spaces(?) which are not allocated.
I mean, cc cc cc cc area.
At first sight it seems not necessary. I wonder why these spaces exist.

minseo
  • 13
  • 3
  • The layout of the memory is up to the compiler. If you run an optimized build of the code, the variables won't be defined at all — they are unused. One reason might be to allow the compiler to detect when your code writes out of bounds — if the 0xCCCCCCCC lines changed, there's a bug in your code. – Jonathan Leffler Jun 11 '23 at 15:54
  • 1
    The behavior is compiler (e.g. gcc vs CLang vs. MVSC) and platform (e.g. Windows/32-bit vs. Linux/x86_64) dependent. SUGGESTION: compile your program with the "generate assembly" flag, e.g. `gcc -S -fverbose-asm myapp.c` – paulsm4 Jun 11 '23 at 15:54
  • Are there three lines of `cc cc cc cc` after `0x0000002FC3EFFA48`? More than 3? – Jonathan Leffler Jun 11 '23 at 15:59
  • @JonathanLeffler Right. 6 lines exist. – minseo Jun 11 '23 at 16:02

1 Answers1

3

This is mainly because VC implements buffer overflow protection by default. Use /GS- command line switch and check again.

Daniel Voina
  • 3,185
  • 1
  • 27
  • 32