Use of STL, friend, reinterpret_cast etc..

Moderators: david ward, misra cpp

Post Reply
vijayendra
Posts: 2
Joined: Tue Jan 17, 2006 3:00 pm
Location: Bangalore, India

Use of STL, friend, reinterpret_cast etc..

Post by vijayendra » Thu May 07, 2009 6:18 pm

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?

misra cpp
Posts: 161
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Use of STL, friend, reinterpret_cast etc..

Post by misra cpp » Wed Oct 07, 2015 9:45 am

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.
Posted by and on behalf of
the MISRA C++ Working Group

Post Reply

Return to “6.11 Member access control (C++)”