0

I get this error:

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

This is the code:

#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]);
}

the strange thing is however, if I set

#define buffersize 10240

everything works. What am I doing wrong?

1
  • running out of stack space? Commented Nov 13, 2013 at 12:44

1 Answer 1

5

On most modern systems automatic variables will be placed on the stack and so you are overflowing your limited stack space. Typical stack sizes are somewhere between 1M to 8M and mbuf is about 10M with a size of 10240000. A better option would be to use dynamic memory allocation or even better since this is C++ you can just use std::vector and not have to worry about deleting dynamically allocated memory when you are done. Stack Overflow Problems covers the typical stack sizes on common systems:

SunOS/Solaris   8172K bytes
Linux           8172K bytes
Windows         1024K bytes
cygwin          2048K bytes
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.