0

我收到此错误:

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) {

这是代码:

#include <fstream>
#include <iostream>
#include <stdlib.h>

#define buffersize 10240000

bool encryptFile(std::string& inPath, std::string& outPath, unsigned char * key)
{
  unsigned char mbuf[buffersize];
}

int main(int argc, char **argv)
{
  unsigned char k[32];
  std::string clear = "clear.bin";
  std::string crypt = "crypt.bin";
  encryptFile(clear, crypt, &k[0]);
}

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

#define buffersize 10240

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

4

1 回答 1

5

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

SunOS/Solaris   8172K bytes
Linux           8172K bytes
Windows         1024K bytes
cygwin          2048K bytes
于 2013-11-13T12:44:19.823 回答