Welcome, Guest
You have to register before you can post on our site.

Username
  

Password
  





  Clarification for 7-3-1: extern "C++"
Posted by: anj - 28-04-2016, 04:21 PM - Forum: 6.7 Declarations (C++) - Replies (2)

For headers included by other C and C++ headers it is vital to use extern "C++".
But the rule implicitly forbids it by only allowing 3 kind of top level declarations.

Is it really intended to use this as the only compliant way:

Code:
extern "C"
{
  extern "C++"
  {
    namespace MY_API
    {
    }
  }
}

Print this item

  Rules 6-5-1 to 6-5-6
Posted by: dg1980 - 28-04-2016, 09:32 AM - Forum: 6.5 Expressions (C++) - Replies (1)

Hi,

i was under the impression that these rules apply to for loops only, as all examples show only for loops.
I recently ran across a static analyzer, which applied these rules to while loops too.
Is this intended by MISRA?
Thanks in advance.

Print this item

  Rules 16-2-1 and 16-2-2
Posted by: dg1980 - 28-04-2016, 09:28 AM - Forum: 6.16 Preprocessing directives (C++) - Replies (2)

Hi,

strictly interpreted, these rules ban conditional compilation, rendering rules 16-1-1 and 16-1-2 mute.
E.g.:

Code:
#ifdef FEATURE_X
// some code
#endif

In addition, there appears to be no such rule in MISRA C 2012 (presumably, because it is a widely used feature in embedded systems):

Rule Mapping to MISRA C 2012
16-0-2 [Required] n.a. but namespaces are not available in C
16-1-1 [Required] n.a.
16-2-1 [Required] n.a.prohibits conditional compilation which is allowed in C
16-2-2 [Required] n.a.prohibits conditional compilation which is allowed in C

I would expect that a feature like the preprocessor, which is equal in C and C++, is treated equally across all MISRA standards.
Is there any chance this will be corrected/clarified in a new version or technical corrigendum?

Print this item

  11.3 - char * to uint8_t *
Posted by: delta_controls - 27-04-2016, 11:05 AM - Forum: 8.11 Pointer type conversions - Replies (5)

In the following example there is an implicit conversion from (char *) to (uint8_t *):

Code:
extern void print(const uint8_t *text);

int main(void)
{
    print("Some text");
}

Am I correct in thinking that this is compliant? Or does the exception to rule 11.3 only apply when conversions are explicit?

Thanks.

Print this item

  Rule 8.4 - main function
Posted by: delta_controls - 20-04-2016, 04:44 PM - Forum: 8.8 Declarations and defnitions - Replies (6)

Should this rule have an exception for main? I can't see how main is automatically excluded from this rule. Is the expectation that a prototype is provided for main?

Thanks.

Print this item

  Rule 19.4 Does "#define XY -1" violate 19.4
Posted by: fst@bm - 17-03-2016, 03:37 PM - Forum: 6.19 Preprocessing Directives - Replies (2)

Dear MISRA Team,

as I found out, 2 out of 3 commercial MISRA checking tools report the statement "#define XY -1" to be a violation of rule 19.4. I am not sure whether or not this #define actually violates rule 19.4 but if so I would not fully understand the risk associated with this particular #define statement. Could someone please help and discuss if this is a violation and why it should be avoided?

Thank your for your support!

Print this item

  Question about 14.2 and the controlling variable
Posted by: triboix - 23-02-2016, 09:43 AM - Forum: 8.14 Control statement expressions - Replies (2)

Hello,

14.2 states that the "controlling variable" must be of essentially boolean type.

Does that mean that the following code is non-compliant?

Code:
struct stuff
{
    uint32_t id;
};
struct stuff stuff_array[MAXSTUFF];

struct stuff* stuff_lookup(uint32_t id)
{
    uint8_t i;
    struct stuff* found = NULL;
    for (i = 0; (i < MAXSTUFF) && (found == NULL); i++) {
        if (stuff_array[i].id == id) {
            found = &(stuff_array[i]);
        }
    }
    return found;
}

Thanks a lot for any help!

Print this item

  How do I create a module in MISRAC:2012 that follows Dir 4.12 and 4.8?
Posted by: crisls1 - 18-02-2016, 08:52 AM - Forum: 7.4 Code design - Replies (2)

I asked the following question on Stack Overflow and received a helpful answer which I have integrated into my question here. (http://stackoverflow.com/questions/35341...12-and-4-8) The following is an extended version of the question I asked. This question relates to coding in ISO C99 following the MISRAC:2012 guidelines.

I am looking for guidance on Dir 4.8 “If a pointer to a structure or union is never dereferenced within a translation unit, then the implementation of the object should be hidden” in conjunction with Dir 4.12 “Dynamic memory allocation shall not be used”.

When implementing an Abstract Data Type in C it is common to refer to the ADT using a handle that is a pointer to a structure describing the internal state of the ADT. This can be done using an opaque pointer as per Dir 4.8 with the benefit that the internal details remain hidden from the user.

Generally more than one of these ADTs could exist so there must be a way to create mulitple handles. This can be solved by allocating memory for the internal details referenced by the handle in an initialisation function, however, this is not allowed under Dir 4.12.

Another option is that the initialisation routine receives a pointer to a statically allocated handle provided by the user, however, this cannot be accomplished using opaque pointers.

I illustrate the problem below.


Module.h

Code:
/* Module handle is only available to  the world as an incomplete type.
           This allows us to satisfy MISRAC 2012 Dir 4.8.*/
        struct module;
        typedef struct module module_t;
Module.c
Code:
#include "module.h"
        struct module
        {
            uint8_t value;
        };
        module_t* module_get_a_handle(void)
        {
             /* MISRAC 2012 Dir 4.12 disallows dynamic memory allocation.*/
             return (module_t*)malloc(sizeof(struct module));
        }
User.c
Code:
#include "module.h"
        module_t* module_handle;
        module_handle = module_get_a_handle();

One solution is to use a statically allocated pool of handles within the ADT that are made available to client code. This solution seems to be technically compliant; however, it seems that the concept of dynamic memory allocation still exists here. I would argue that although the handles are statically allocated, it cannot be determined by the compiler during compilation whether there will be enough handles available for the software to function correctly.

I understand the argument that if a module runs out of statically allocated handles then it is a design error. However, it seems advantageous if the compiler can also determine in many cases whether there is enough space at compile time (this forces developers to fix the problem early rather than discovering it in code reviews or testing later on). The compiler can determine the problem if a statically allocated handle is passed from client code to the ADT as it is then clear to the compiler how many handles in total will be required by the SW. This also feels like a cleaner separation of concerns to me. Is it the responsibility of the ADT to know how many handles may be required, and if so does this not complicate any module reuse somewhat?

Additionally, in my case we are defining a company template that all new modules should be based on. The level of C proficiency of our developers ranges from beginner to intermediate. I am concerned that requiring each ADT to manage a static buffer of handles will be seen as overly complicated (and therefore a maintenance issue) and be met with resistance. Clearly raising the C skill level of developers is desirable but this will not happen immediately.

My solution to this problem would be to deviate on the advisory directive 4.8 and use non-opaque pointers and a strong naming convention that makes it clear to users that the internal details of the ADT must not be changed.

I am curious whether there is a well accepted method to solve this issue that satisfies Dir 4.8, and Dir 4.12, and does not break any other MISRAC:2012 rules. Is it common practice to define a static array of handles in every ADT?

I'm also interested in a critique of

a) deviating Dir 4.8 and using a naming convention that makes it obvious that the handle points to internal details that should not be accessed directly coupled with educating developers not to access the internals details.

b) deviating Dir 4.12(!) to allow dynamic memory allocation in a well defined initialisation phase only. For b), I believe that if it can be shown that the memory allocation is deterministic in this initialisation phase and the initialisation phase can be tested at a system level successfully once then it follows that the memory allocation will always be successful.

My preference is to deviate on the advisory directive 4.8 in this particular case as it seems to have the least disadvantages.

Any comments would be greatly appreciated.

Print this item

  MISRA C:2012 not available
Posted by: SimonB - 17-02-2016, 04:27 PM - Forum: General Questions - Replies (9)

The printed version of MISRA C:2012 is shown as "This document is temporarily out of stock" in the on-line shop http://www.misra.org.uk/shop/buy_now.php. Does anybody know when it is likely to be in print again?

The printed form has the advantage that it can be shared around the office, rather than having to pay for PDF copies for everybody involved.

Print this item

  Clarity of Rule 14-6-1
Posted by: Insane Vent Storm - 17-02-2016, 01:21 PM - Forum: 6.10 Derived classes (C++) - Replies (1)

Rule 14-6-1 says 'In a class template with a dependant base, any name that may be found in that dependant base shall be referred to using a qualified-id or this->'. Does the use of the word 'template' mean that the rule applies to template classes, or does the rule apply to all inheriting classes ?

Print this item

Search Forums

(Advanced Search)

Forum Statistics
» Members: 6,309
» Latest member: 1fuhd
» Forum threads: 1,066
» Forum posts: 2,933

Full Statistics

Online Users
There are currently 112 online users.
» 0 Member(s) | 108 Guest(s)
Bing, Facebook, Google, UptimeRobot

Latest Threads
13.2 Was TC2 correctly ap...
Forum: 8.13 Side effects
Last Post: misra-c
08-10-2026, 12:47 PM
» Replies: 1
» Views: 625
20.12 Does the stringific...
Forum: 8.20 Preprocessing directives
Last Post: misra-c
08-10-2026, 12:40 PM
» Replies: 1
» Views: 554
13.3 Should side effects ...
Forum: 8.13 Side effects
Last Post: misra-c
08-10-2026, 12:37 PM
» Replies: 1
» Views: 428
11.6 pointer to pointer t...
Forum: 8.11 Pointer type conversions
Last Post: misra-c
08-10-2026, 12:21 PM
» Replies: 2
» Views: 1,032
What is the UTLR of -1?
Forum: 8.10 The essential type model
Last Post: misra-c
08-10-2026, 12:15 PM
» Replies: 1
» Views: 597
10.3 Clarify if narrower ...
Forum: 8.10 The essential type model
Last Post: misra-c
08-10-2026, 11:57 AM
» Replies: 1
» Views: 508
It 8.12 intended to be so...
Forum: 8.8 Declarations and defnitions
Last Post: misra-c
08-10-2026, 11:50 AM
» Replies: 1
» Views: 455
Rule 0.1.1 Example f4
Forum: 4.0 Language independent issues
Last Post: cgpzs
07-10-2026, 11:07 AM
» Replies: 3
» Views: 726
Rule 6-2-2 Usage of std::...
Forum: 6.6 Statements (C++)
Last Post: misra cpp
25-09-2026, 05:43 PM
» Replies: 1
» Views: 437
M5-0-4 switch over unscop...
Forum: 6.5 Expressions (C++)
Last Post: misra cpp
25-09-2026, 01:00 PM
» Replies: 3
» Views: 539