BrainF**k interpreter in C++ This question is the adaptation of this question in C++. (I'm the author of that code as well).

The code:

#include &lt;bits/stdc++.h&gt;

std::string bf_interpreter(std::string code){
    long long int ptr = 0;

    std::vector&lt;int&gt; values = {0};
    long long int length = 1;

    std::vector&lt;int&gt; brackets;

    std::string result = "";

    for(unsigned long long int index = 0; index &lt; code.size(); index++){
        while(length &lt;= ptr){
            length++;
            values.push_back(0);
        }

        char character = code[index];

        switch(character){
            case '&gt;': ptr++; break;
            case '&lt;': ptr--; break;

            case '+': values[ptr] = (values[ptr] + 1) % 256; break;
            case '-': values[ptr] = (values[ptr] - 1) % 256; break;

            case '[':
                brackets.push_back(index);
                break;

            case ']':
                if(values[ptr] == 0)
                    brackets.pop_back();
                else
                    index = brackets[brackets.size() - 1];

                break;

            case '.':
                result += (char)values[ptr];
                break;

            case ',':
                char input; std::cin &gt;&gt; input;
                values[ptr] = (int)input;
                break;
        }
    }

    return result;
}


int main()
{
    std::string s;

    std::cin &gt;&gt; s;

    std::cout &lt;&lt; bf_interpreter(s);
}


I'm quite a beginner in C++, so please excuse me if I'm not following any standard conventions.
Bug

The only issue I see is that the data array is never expanded.

As a result any value of ptr that is not zero will cause undefined behavior. What you want to do is give a fixed but reasonable size for the data.

std::size_t const maxDataSize = 10000;

...

   std::vector&lt;int&gt; values;
   values.resize(maxDataSize);


Then make sure when you access the data you mod it to the correct range:

        case '&gt;': ptr = (ptr + 1) % maxDataSize; break;
        case '&lt;': ptr = (ptr - 1) % maxDataSize; break;


Comment

This is not a standard C++ header.

#include &lt;bits/stdc++.h&gt;


What you want is

#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;


Comment

The code is non modifiable so the the code parameter should be constant. I would also pass by const reference to prevent a copy or require a move operation to pass the code into the interpretor.



I can see how you would interpret the output as what is printed to the output stream by the interpreter as the result. But is not the whole of memory the actual output?



Two things I would do:


Make . output to the output stream (like , takes from the input stream.
I would give the bf_interpreter() two parameters. The code to execute and the data memory that can be used by the program. The data memory is passed by reference so that any changes can be inspected after the interpreter has finished.
Potentially I would even pass two streams (input/output) as parameters to bf_interpreter(). Though that is a bit more subjective.


Thus my function definition would be this:

 std::string bf_interpreter(std::string const&amp; code,
                            std::vector&lt;int&gt;&amp;  memory,
                            std::istream&amp;      input = std::cin,
                            std::ostream&amp;      output = std::cout
                           );


ReDesign

OK. So given there is a lot more state here than I really want to pass as a function. I would change this a bit and create a "Brain Fuck" class where all the important state is passed to the constructor then you can call interpret on the object.

class BF_Engine
{
         // Add Members here
         void nextStep() {
             // Add interpreter here
         }
    public:
         BF_Engine(std::string const&amp; code,
                   std::vector&lt;int&gt;&amp;  memory,
                   std::istream&amp;      input = std::cin,
                   std::ostream&amp;      output = std::cout
                  );

         void interpret(std::size_t steps = 1)
         {
             while (!finished &amp;&amp; steps &gt; 0) {
                 nextStep();
                 --steps;
             }
         }
         void interpretRun()
         {
             while (!finished) {
                 nextStep();
             }
         }
 };