Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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?
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
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.