Main Content

CERT C: Rec. PRE03-C

Prefer typedefs to defines for encoding non-pointer type

Since R2024a

Description

Rule Definition

Prefer typedefs to defines for encoding non-pointer type.1

Polyspace Implementation

The rule checker checks for the issue Nonpointer type encoded in macro.

Examples

expand all

Issue

The issue occurs when a nonpointer type is encoded as a macro using a #define preprocessor directive instead of by using a typedef. The rule checker does not report a violation if:

  • Pointer types are encoded in a macro.

  • You encode qualifiers such as static or volatile alongside types in a macro. These qualifiers are not part of the type definition and cannot be used in a typedef statement.

  • A macro that encodes nonpointer types is not used in the code.

Risk

Macro definitions do not obey scope rules, and types encoded in macros are applied to the code using textual substitution. Best practice is to encode a nonpointer type using a typedef.

Fix

Use a typedef to encode a nonpointer type instead of using a macro.

Example — Types encoded in macros

In this example, the rule checker reports violations on macros that encode nonpointer types.

#define MATRIX(X) double X[4][4]  //Noncompliant
#define MY_INTEGRAL_TYPE1(sign, base) sign base  //Noncompliant
#define QUAL_TYPE_MAC1 const int  //Unused - no violation reported
#define QUAL_TYPE_MAC3 static volatile int //Not a violation
                  // - volatile and static cannot be used with typedefs

void noncompliant ()
{
    MATRIX(tab);
	MY_INTEGRAL_TYPE1(unsigned, int) uint;
	QUAL_TYPE_MAC3 var3;
}

Unused macros and macros that encode qualifiers such as volatile or static do not violate this rule.

Correction — Use typedef statements to encode nonpointer types

In this compliant example, the nonpointer types are encoded using typedef statements.

typedef double matrix[4][4];
typedef unsigned int myint;

void compliant ()
{
    matrix matrix_a;
	myint uint;
}

Check Information

Group: Rec. 01. Preprocessor (PRE)

Version History

Introduced in R2024a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.