#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
size_t sz = atol(argv[1]);
char *arr = malloc(sz);
sleep(10);
}
Run Code Online (Sandbox Code Playgroud)
我编译了这段代码并尝试运行它,用于pmap查看程序的内存映射。
当我使用一些大数字时1024000,我会得到这样的映射:
3901: ./alloc_program 1024000
0000560192f43000 4K r---- alloc_program
0000560192f44000 4K r-x-- alloc_program
0000560192f45000 4K r---- alloc_program
0000560192f46000 4K r---- alloc_program
0000560192f47000 4K rw--- alloc_program
0000560192fac000 132K rw--- [ anon ]
00007f75b69e9000 1004K rw--- [ anon ] <---- I believe this is the allocated memory
00007f75b6ae4000 148K r---- libc-2.31.so
00007f75b6b09000 1504K r-x-- libc-2.31.so
00007f75b6c81000 …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <malloc.h>
void print_vals(int n)
{
int *arr = (int *)alloca(n);
for (int i = 0; i < n; i++)
arr[i] = i;
for (int i = 0; i < n; i++)
std::cout << arr[i] << ' ';
std::cout << '\n';
}
int main()
{
print_vals(5);
print_vals(10);
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,每次调用都会收到此错误:
Run-Time Check Failure #4 - Stack area around _alloca memory reserved by this function is corrupted
Run Code Online (Sandbox Code Playgroud)
我使用的是 Visual C++ 2019,stdc++14 和 stdc++17 都会产生相同的错误。
这段代码有什么问题?
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
char ch = '?';
System.out.println((int)ch);
int c;
while ((c = System.in.read()) != -1)
{
System.out.println(c);
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
35830
Run Code Online (Sandbox Code Playgroud)
这里,?Unicode中表示字符的值是35830. 在二进制中,它将是10001011 11110110.
当我在终端中输入该字符时,我希望得到两个字节,10001011并且11110110. 当再次组合它们时,我可以获得原始字符。
但我实际得到的是:
232
175
182
10
Run Code Online (Sandbox Code Playgroud)
我可以看到10代表换行符。但是前3个数字是什么意思?
从这个维基:

它指出偏移量 7 处的数字标识目标操作系统。
我已经为 linux 机器编译了 ac 程序并检查elf header了结果文件的(前 64 个字节):
> xxd -l 64 helloworld
00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............
00000010: 0300 3e00 0100 0000 8012 0000 0000 0000 ..>.............
00000020: 4000 0000 0000 0000 183f 0000 0000 0000 @........?......
00000030: 0000 0000 4000 3800 0d00 4000 1f00 1e00 ....@.8...@.....
Run Code Online (Sandbox Code Playgroud)
为什么我得到01第 7 个字节?不应该03吗?