C++ Iterators doing weird things

91 views Asked by At

I might be completely stupid, but:

class Lexer {
    private:
        std::string::iterator start;
        std::string::iterator current;
        std::string source;
        int line;
        std::unordered_map<std::string, TokenType> keyword_table;

    public:
        Lexer(std::string program){
            this->source = program;
            this->start = program.begin();
            this->current = program.begin();
            
            this->line = 1;

            std::cout << std::distance(this->start, this->source.begin()) << std::endl;             
        }
        
        Token scan();      
};

When I print the distance between this->start and this->source.begin(), i get some wild number. Depending on the string that I've fed into the class, I've gotten 40 and even 448. I'm very confused on why this is.

I've tried changing variable names, switching between const_iterator and iterator, and printing this->source, program, this->start, etc. I'm very confused about why this problem is occuring. Btw the language is c++. Am I using std::distance wrong?

1

There are 1 answers

0
Slava On

This line of code:

this->source = program;

Makes variable source contain copy of data of variable program. The fact that both variables contain the same string after that does not mean that their iterators would be related at all. Your code could be simplified as this:

std::string s1 = "abc";
std::string s2 = s1;
std::cout << std::distance( s1.begin(), s2.begin() ) << "\n";

Which is invalid and leads to UB as you cannot calculate distance btw unrelated iterators as stated in documentation:

If InputIt is LegacyRandomAccessIterator, the behavior is undefined if first and last are neither reachable from each other.

(Despite the fact that you store dangled iterator in start and current after you ctor terminates which is a different issue)