|Titan Architecture internals: On why index operators in the generated code have signed parameter [message #1747584]
||Wed, 16 November 2016 09:07
| Kristof Szabados
Registered: July 2009
There is a question repeated often by developers who work join the Titan development team: why is the parameter of indexing operators in the generated code always signed?|
At first this might not make too much sense: most of the time valid indexes can only be positive integers and 0.
Using an unsigned int type would sound reasonable, but it easy to show in a few steps why that would not be a good idea.
Whenever a piece of C++ code is generated or made available in the Titan libraries ... we have to assume that user will use (maybe even misuse) them.
This is not unexpected. In external functions, users have to work with our code on the C/C++ level.
for example: to read from an input into a charstring in an external function ... one has to work with the Charstring class directly.
The TTCN-3 standard requires that the language rules are kept in each point of the execution.
This has nothing to do with compile time checking ... that does not protect against corrupted data appearing at runtime, misconfiguration, etc...
So it is required to check several rules in the runtime.
For example: if the received function parameter bound, is the index parameter a negative number or overflowing.
And we are already at the point where the implementation of index operators have to check it's parameter for boundness and being within bounds, before doing any operations with it.
We also need pay attention to the error messages reported by Titan.
Most of the time users are working with large and complex systems, within strict timelimits ... and might not be experts in the details of C/C++ memory management details.
So when the -1th element of a record of/set of/charstring/etc.. is requested it is preferable when the error message also mentions the -1 number.
But if we use unsigned int types, the C/C++ compiler would implicitly convert, resulting in:
- in best case an error message telling that we are trying to index the 2^64-1 -th element of a much shorter structure.
- in worst the runtime might try to allocate 2^64-1 *(some amount) memory to extend the data structure.
Powered by FUDForum
. Page generated in 0.01332 seconds