I am trying to build an Assembler in C++, but when I return from a function (itoa), I get a seg. fault before I can print anything out. The output makes it obvious that the issue is between the end of the function and the next line of code in the calling function. I have tried returning std::string and std::string* instead of doing parameters by reference, but nothing has worked. I have no clue where it is coming from, so any help on where to look would be great. Below is the relevant code and output:

void Assembler::assembleCode(std::string assembly) {
...
std::string imm;
std::cout << "14\n";
itoa(labelPositions[tokens[3]], 16, imm);
std::cout << "13\n";
...
}

void Assembler::itoa(int value, int len, std::string& imm) {
...
imm = std::string(binresult.substr(30 - len, len));
std::cout << "Translated to " << imm << std::endl;
}

Output:
....
14
....
Translated to 0000000000011111
Segmentation Fault