为什么在C中分配的堆栈内存中有空白空间?

huangapple go评论49阅读模式
英文:

Why are there empty spaces among stack memory allocated in C?

问题

The code you provided appears to be in C or C++. It seems that you are experiencing non-sequential memory allocation with some areas filled with cc cc cc cc. The presence of these spaces is likely due to how memory allocation and initialization work in your program. The cc cc cc cc values are often used to represent uninitialized memory or padding.

The reason for non-sequential memory allocation and the presence of these spaces can be attributed to various factors, including compiler optimizations, alignment requirements, and the specific memory allocation strategy used by your system.

To understand the exact reasons for this behavior, you may need to delve into the compiler settings, optimization options, and the memory allocation strategy of your development environment (Microsoft Visual Studio 2022 in this case). It's also possible that the compiler is introducing padding for alignment purposes, which is a common practice to ensure efficient memory access.

If you want to investigate further, you may consider examining compiler documentation or consulting with experts familiar with memory management and compiler internals.

英文:

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,<br>
and when I check my memory allocated with debugger,<br>
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<br>
and there exist spaces(?) which are not allocated.<br>
I mean, cc cc cc cc area.<br>
At first sight it seems not necessary. I wonder why these spaces exist.<br>

答案1

得分: 3

这主要是因为VC默认实现了缓冲区溢出保护。使用/GS-命令行开关并再次检查。

英文:

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

huangapple
  • 本文由 发表于 2023年6月11日 23:50:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定