About initializing aggregates with pointer variables in MISRA 2020 Rule 11.9 members (macro NULL or 0)

Asked 2 years ago, Updated 2 years ago, 40 views

In MISRA 2020 Rule 11.9, "Macro NULL must only be allowed in the form of an integer null pointer constant."

Is it exceptionally OK to initialize this rule with 0 when initializing a aggregation with pointer variables as a member?

"I think ""macro NULL"" is better, but if there is a clear description in the specification, I would like it to be developed."

Note: One of the applicable conditions is "as a value to be replaced by a pointer", so I think the following method is also related.

Example:

structure S{
 int*p;
}
void func(void){
 structureSp={0};
}

c

2022-09-30 20:23

1 Answers

If you are interested in the specification, please purchase it.I have purchased Oira.I'm asking you to quote, but you may not be able to get the full picture to the extent that it's only quoted.

JIS X 3010:2003 (ISO/IEC 9899:1999) 6.3.2.3 pointer
An integer constant expression having a value 0 or an expression obtained by casting the constant expression into a type void* is called an empty pointer constant.

The meaning of this phrase is "Integer literal 0 is arbitrarily converted to an empty pointer constant when it is obvious that it needs to be done."In other words, char*p=0; is legitimate in the specification and p is an empty pointer (nullptr).The same is true for aggregates, where p.p is initialized with an empty pointer constant.

For example, printf("%p\n", 0); is not obvious to the compiler that 0 must be converted to an empty pointer constant.This printf() will not produce the expected behavior for processing systems with different sizes of int.

The rest is the structural initialization that includes pointer variables in the linked C language.

As a result of editing the question, the content has changed completely, so I will add the changes (change further)

MISRA is an alternative to "prohibiting anything that could cause bugs even if the language standard allows it."This is exactly where you can convert the constant expression that guides 0 of int to nullptr at compile time.

  • Language Standards Accepted
  • MISRA doesn't approve (Leave document if you want to deviate)

That's all.Therefore, MISRA is issuing a warning to the presented source code that is completely legitimate in terms of


2022-09-30 20:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.