获取地址错误时无法访问内存

Ral*_*alf 0 c++ gdb

我收到此错误:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400f8b in encryptFile (
inPath=<error reading variable: Cannot access memory at address 0x7fffff63a0f8>, 
outPath=<error reading variable: Cannot access memory at address 0x7fffff63a0f0>, 
key=<error reading variable: Cannot access memory at address 0x7fffff63a0e8>) at    main3.cpp:7
7   bool encryptFile(std::string& inPath, std::string& outPath, unsigned char * key) {
Run Code Online (Sandbox Code Playgroud)

这是代码:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400f8b in encryptFile (
inPath=<error reading variable: Cannot access memory at address 0x7fffff63a0f8>, 
outPath=<error reading variable: Cannot access memory at address 0x7fffff63a0f0>, 
key=<error reading variable: Cannot access memory at address 0x7fffff63a0e8>) at    main3.cpp:7
7   bool encryptFile(std::string& inPath, std::string& outPath, unsigned char * key) {
Run Code Online (Sandbox Code Playgroud)

然而,奇怪的是,如果我设置

#define buffersize 10240
Run Code Online (Sandbox Code Playgroud)

一切正常。我究竟做错了什么?

Sha*_*our 5

在大多数现代系统上,自动变量将被放置在堆栈中,因此您会溢出有限的堆栈空间。典型堆栈尺寸是介于1M to 8Mmbuf是大约10M大小为10240000。更好的选择是使用动态内存分配,甚至更好,因为这是C++,您可以只使用std::vector而不必担心在完成后删除动态分配的内存。堆栈溢出问题涵盖了常见系统上的典型堆栈大小:

SunOS/Solaris   8172K bytes
Linux           8172K bytes
Windows         1024K bytes
cygwin          2048K bytes
Run Code Online (Sandbox Code Playgroud)