MISRA Discussion Forums

Full Version: Use of STL, friend, reinterpret_cast etc..
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
MISRA C++ Guidelines does not clearly mention about the following C++ constructs:
1. Use of STL (cstring, vector etc) which are internally based on dynamic allocations
2. Use of friend keyword to access private members of another class
3. Use of reinterpret_cast
4. Use of struct rather than classes - in case the set of data does not have specific operations on it, hence no need to encapsulate them in a class
5. Casting from struct to byte pointer - usually needed to send set of data like array of struct
6. Use of string functions which are not unbounded, e.g. strncpy, memcpy, bcopy, memset etc. All these functions accept size as an argument and access only the given area in memory.

Can you pl. clarify the guidelines on the above?
Question 1
Rule 18.4.1 states that dynamic memory allocation shall not be used. This rule also applies to 'normal' usage of the STL containers. STL types could potentially be used with a custom allocator that did not require dynamic memory allocation.

Question 2
MISRA C++ does not have any guidelines on the use of friends.

Question 3
MISRA C++ includes rules on cast expressions, for example 5.2.6.

Question 4
A struct is a class that is implicitly public rather than private. Rules such as 6.9.x therefore apply to all classes, irrespective of the class-key used in their declaration.

Question 5
A deviation would be required for such a construct.

Question 6
Bounded string functions are better than the unbounded versions, however, as per 18-0-5 it is recommended that a safe string handling library is used.