注意:这篇文章上次更新于1583天前,文章内容可能已经过时。
This article was last updated1583 days ago, the content may be outdated.
A Baidu Written Test Question
Q: What does the following program output?
- A: 1
- B: 2
- C: 3
- D: It won’t compile
1 |
|
正确答案是…
The correct answer is…
C
如果和你想的一样,那说明你已经掌握了这个知识点了。
第一眼看本来想选 D 的(我是个呆瓜🐽),后来想了一下(就是想了一下而已),决定选 A。
我对这道题还是比较好奇,于是笔试完用 OnlineGBD 敲了一下。
结果竟然是…!!!

一时之间不知道为什么,并且表示很吃惊,于是和同学讨论一下。

让我更是震惊,难道天底下只有我一个呆瓜吗😂

又简单聊几句勾起了我大脑深处尘封已久的对于知识的记忆。我想起了栈区地址和堆区地址生长方向的问题,但是记不清楚栈区是低地址向高地址生长还是高地址向低地址生长。又联想前大家总是说压栈,难道是低地址向高地址生长?可是和输出结果不相符啊。
于是简单测试一下。

结论很显然:
栈区向低地址生长,堆区向高地址生长
C
If you got the same answer, it means you’ve already mastered this knowledge point.
At first glance I wanted to choose D (I’m a dumbbell 🐽), but after thinking about it (just thinking about it a little), I decided on A.
I was still curious about this problem, so after the written test I tried it out on OnlineGDB.
And the result was…!!!

For a moment I couldn’t figure out why, and I was quite shocked, so I discussed it with my classmates.

Even more shocking — am I the only dumbbell in the world? 😂

A few more words of chat stirred up the long-buried knowledge in the depths of my brain. I remembered the question of which direction stack and heap addresses grow, but I couldn’t recall whether the stack grows from low addresses to high addresses or from high addresses to low addresses. Then I thought about how people always talk about pushing onto the stack — could it grow from low addresses to high addresses? But that doesn’t match the output.
So I did a quick test.

The conclusion is obvious:
The stack grows toward lower addresses, and the heap grows toward higher addresses
More Tests
Since I’d already tested this far, I might as well review more thoroughly and test all kinds of variables.
1 |
|
代码的运行结果如下:
The output of the code is as follows:
1 | 栈区地址: |
可以得到几个基本结论:
- 栈区地址向下生长
- 堆区地址向上生长
- 局部静态变量和全局静态变量是放在一起的
- 字符串常量、静态变量和全局 const 常量处于整个内存空间的低地址,其余处于高地址。
由于局部 const 常量的生长方向是向上的,因此怀疑其可能存放在堆区(网上说其存放在栈区)。
A few basic conclusions can be drawn:
- The stack area grows downward
- The heap area grows upward
- Local static variables and global static variables are stored together
- String constants, static variables, and global const constants are located at low addresses in the memory space, while the rest are at high addresses.
Since the growth direction of local const constants is upward, it’s suspected that they may be stored in the heap area (the internet says they’re stored in the stack area).
总结
C/C++中内存分5大区:栈,堆,全局/静态存储区,常量存储区,代码区

Code Segment 代码区也称Text Segment,存放可执行程序的机器码。
Data Segment 存放已初始化的全局和静态变量, 常量数据(如字符串常量)。
BSS(Block started by symbol) 存放未初始化的全局和静态变量。(默认设为0)
Heap(堆) 从低地址向高地址增长。容量大于栈,程序中动态分配的内存在此区域。
Stack 栈从高地址向低地址增长。由编译器自动管理分配。程序中的局部变量、函数参数值、返回变量等存在此区域。
Summary
In C/C++, memory is divided into 5 major areas: stack, heap, global/static storage, constant storage, and code area

Code Segment (代码区), also known as the Text Segment, stores the machine code of the executable program.
Data Segment stores initialized global and static variables, and constant data (such as string constants).
BSS (Block started by symbol) stores uninitialized global and static variables. (Set to 0 by default)
Heap grows from low addresses to high addresses. Its capacity is larger than the stack; dynamically allocated memory in the program resides in this area.
Stack grows from high addresses to low addresses. It is automatically managed and allocated by the compiler. Local variables, function parameter values, return variables, etc. reside in this area.


