Sunday, September 7, 2014

RPN List – Default Move Not Sufficient

The RPN list class contains other members in addition to the list including the code size, error column, error length and error message members.  Since the list member is a standard list with a move operation, it is cleared by the default generated move constructor when calling the standard move in the return statement of the translate routine.  All but the error message are integers with no default move operation so these members are not affected.  The error message is currently a QString, which also does not have a move operation (the Qt5 classes do) and was also not affected.

When a new RPN list instance was allocated at the beginning of the translate routine, these other  members were initialized according to the constructor (though only the error members were initialized).  Now, at the return statement, the list of the RPN list member is cleared from the default move, but with no move operations for the other members, these members retained their values.

This was detected while running the tests.  Once a line with an error was reported, that same error was reported for every line afterward (at least until there was a different error).  This occurred because the error column member (used to test if the RPN list has an error) was not initialized for the next translation.

One solution would be to reset these other members at the at the beginning where the allocation was.  The better solution of course is to add move constructor and assignment functions.  The move constructor initializes each of the members from the other RPN list instance.  In the function body, each of the members of the other RPN list instance are set to an initialized state.  The list and error message members are cleared and the integer variables are set to appropriate default values.  The move assignment swaps all the members before returning a reference to the instance.

No comments:

Post a Comment

All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)