== Why is this an issue?

Flexible array members are most likely to be used in conjunction with dynamic memory allocation.


The presence of flexible array members modifies the behaviour of the ``++sizeof++`` operator in ways that might not be expected by a programmer. The assignment of a structure that contains a flexible array member to another structure of the same type may not behave in the expected manner as it copies only those elements up to but not including the start of the flexible array member.


=== Noncompliant code example

[source,cpp]
----
#include <stdlib.h>
struct s
{
  uint16_t len;
  uint32_t data[ ]; // Noncompliant - flexible array member
} str;

struct s *copy ( struct s *s1 )
{
  struct s *s2 = malloc ( sizeof ( struct s ) + ( s1->len * sizeof ( uint32_t ) ) );
  /* Omit malloc ( ) return check for brevity */
  *s2 = *s1; /* Only copies s1->len */
  return s2;
}
----


== Resources

* MISRA C:2012, 18.7 - Flexible array members shall not be declared.


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Remove this flexible array member.


'''
== Comments And Links
(visible only on this page)

=== on 5 Jan 2015, 12:59:50 Samuel Mercier wrote:
I created this rule from MISRA 2012. Can you verify? Thx

=== on 6 Jan 2015, 19:31:33 Ann Campbell wrote:
\[~samuel.mercier], I'm not sure I'd have made it on by default, but other than that it looks good.

endif::env-github,rspecator-view[]
