![]() |
|
MISRA-C:2004 Rule 18.3: (Required) |
|
||||
![]() | |||||||
This practice is not recommended for safety-related systems as it brings with it a number of dangers. For example: a program might try to access data of one type from the location when actually it is storing a value of the other type (e.g. due to an interrupt). The two types of data may align differently in the storage, and encroach upon other data. Therefore the data may not be correctly initialised every time the usage switches. The practice is particularly dangerous in concurrent systems.
However it is recognised that sometimes such storage sharing may be required for reasons of efficiency. Where this is the case it is essential that measures are taken to ensure that the wrong type of data can never be accessed, that data is always properly initialised and that it is not possible to access parts of other data (e.g. due to alignment differences). The measures taken shall be documented and justified in the deviation that is raised against this rule.
This might be achieved by the use of unions, or various other means.
Note that there is no intention in the MISRA-C guidelines to place restrictions on how a compiler actually translates source code as the user generally has no effective control over this. So for example, memory allocation within the compiler whether dynamic heap, dynamic stack or static, may vary significantly with only slight code changes even at the same level of optimisation. (Note also that some optimisation may legitimately take place even when the user makes no specific request for this.)
Example Code:
/* PRQA S 3206,3218 ++ */
#include "misra.h"
#include "m2cmex.h"
#define BSPACE 10000
static PC text_buffer[ BSPACE ];
static void process_commands( PC *work );
static void process_report( PC *work );
extern S16 test_1803( void )
{
/* Use text_buffer for command processing purposes. */
process_commands( text_buffer );
/* Use text_buffer for report processing purposes */
process_report( text_buffer );
return 1;
}
static void process_commands( PC *work )
{
/* Use work to process commands */
}
static void process_report( PC *work )
{
/* Use work to process report */
}
No QAC messages cover this rule.
|
||
(c) The Motor Industry Research Association, 2004
|
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2 © 2013 Programming Research www.programmingresearch.com |
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index | Contents |