C and C++ Data Types. The Portland Group PGI Compiler
Tangazo
Tangazo
C and C++ Data Types
Fortran 90 Aggregate Data Types (Derived Types)
The Fortran 90 standard added formal support for aggregate data types. The TYPE statement begins a derived type data specification or declares variables of a specified user-defined type. For example, the following would define a derived type ATTENDEE:
TYPE ATTENDEE
CHARACTER(LEN=30) NAME
CHARACTER(LEN=30) ORGANIZATION
CHARACTER (LEN=30) EMAIL
END TYPE ATTENDEE
In order to declare a variable of type ATTENDEE and access the contents of such a variable, code such as the following would be used:
TYPE (ATTENDEE) ATTLIST(100)
. . .
ATTLIST(1)%NAME = ‘JOHN DOE’
C and C++ Data Types
C and C++ Scalars
Table 13.4, “C/C++ Scalar Data Types”
lists C and C++ scalar data types, providing their size and format.
shows scalar alignments that apply to individual scalars and to scalars that are elements of an array or members of a structure or union. Wide characters are supported (character constants prefixed with an L). The size of each wide character is 4 bytes.
Data Type
Table 13.4. C/C++ Scalar Data Types
Size
(bytes)
unsigned char
[signed] char unsigned short
[signed] short unsigned int
[signed] int
[signed] long [int] (32-bit operating systems and win64)
4
[signed] long [int]
(linux86-64 and sua64)
8
4
4 unsigned long [int] (32-bit operating systems and win64)
4
8 unsigned long [int]
(linux86-64 and sua64)
[signed] long long [int] 8
1
1
2
2
Format
ordinal
Range
0 to 255
2's complement integer -128 to 127 ordinal 0 to 65535
2's complement integer -32768 to 32767 ordinal 0 to 2
32
-1
2's complement integer -2
31
to 2
31
-1
2's complement integer -2
31
to 2
31
-1
2's complement integer -2 ordinal ordinal
0 to 2
0 to 2
2's complement integer -2
63
63
to 2
32
64
to 2
63
-1
-1
63
-1
-1
154
Chapter 13. Fortran, C, and C++ Data Types
Data Type
unsigned long long [int] float double long double bit field
(2)
(unsigned value) 1 to 32 bit field
(2)
(signed value) pointer enum
8
4
Size
(bytes)
8
8 bits
1 to 32 bits
4
4
Format Range
ordinal
IEEE single-precision floating-point
0 to 2
64
-1
10
-37
to 10
38
(1)
IEEE double-precision floating-point
10
-307
to 10
308
(1)
IEEE double-precision floating-point
10
-307
to 10
308
(1) ordinal 0 to 2 size
-1, where size is the number of bits in the bit field
2's complement integer -2 size-1
to 2 size-1
-1, where size is the number of bits in the bit field address 0 to 2
32
-1
2's complement integer -2
31
to 2
31
-1
(1)
Approximate value
(2)
Bit fields occupy as many bits as you assign them, up to 4 bytes, and their length need not be a multiple of 8 bits (1 byte)
Table 13.5. Scalar Alignment
Data Type
char short int enum pointer float double long double long [int] 32-bit on Win64 long [int] linux86-64, sua64 long long [int]
Alignment on this size boundary
1-byte boundary, signed or unsigned.
2-byte boundary, signed or unsigned.
4-byte boundary, signed or unsigned.
4-byte boundary.
4-byte boundary.
4-byte boundary.
8-byte boundary.
8-byte boundary.
4-byte boundary, signed or unsigned.
8-byte boundary, signed or unsigned.
8-byte boundary, signed or unsigned.
C and C++ Aggregate Data Types
An aggregate data type consists of one or more scalar data type objects. You can declare the following aggregate data types:
155
C and C++ Data Types
array consists of one or more elements of a single data type placed in contiguous locations from first to last.
class
(C++ only) is a class that defines an object and its member functions. The object can contain fundamental data types or other aggregates including other classes. The class members are allocated in the order they appear in the definition but may not occupy contiguous locations.
struct is a structure that can contain different data types. The members are allocated in the order they appear in the definition but may not occupy contiguous locations. When a struct is defined with member functions, its alignment rules are the same as those for a class.
union is a single location that can contain any of a specified set of scalar or aggregate data types. A union can have only one value at a time. The data type of the union member to which data is assigned determines the data type of the union after that assignment.
Class and Object Data Layout
Class and structure objects with no virtual entities and with no base classes, that is just direct data field members, are laid out in the same manner as C structures. The following section describes the alignment and size of these C-like structures. C++ classes (and structures as a special case of a class) are more difficult to describe. Their alignment and size is determined by compiler generated fields in addition to user-specified fields. The following paragraphs describe how storage is laid out for more general classes. The user is warned that the alignment and size of a class (or structure) is dependent on the existence and placement of direct and virtual base classes and of virtual function information. The information that follows is for informational purposes only, reflects the current implementation, and is subject to change. Do not make assumptions about the layout of complex classes or structures.
All classes are laid out in the same general way, using the following pattern (in the sequence indicated):
• First, storage for all of the direct base classes (which implicitly includes storage for non-virtual indirect base classes as well):
• When the direct base class is also virtual, only enough space is set aside for a pointer to the actual storage, which appears later.
• In the case of a non-virtual direct base class, enough storage is set aside for its own non-virtual base classes, its virtual base class pointers, its own fields, and its virtual function information, but no space is allocated for its virtual base classes.
• Next, storage for the class’s own fields.
• Next, storage for virtual function information (typically, a pointer to a virtual function table).
• Finally, storage for its virtual base classes, with space enough in each case for its own non-virtual base classes, virtual base class pointers, fields, and virtual function information.
156
Chapter 13. Fortran, C, and C++ Data Types
Aggregate Alignment
The alignment of an array, a structure or union (an aggregate) affects how much space the object occupies and how efficiently the processor can address members.
Arrays align according to the alignment of the array elements. For example, an array of short data type aligns on a
2-byte boundary.
Structures and Unions align according to the most restrictive alignment of the enclosing members. For example the union un1 below aligns on a 4-byte boundary since the alignment of c, the most restrictive element, is four: union un1 {
short a; /* 2 bytes */
char b; /* 1 byte */
int c; /* 4 bytes */
};
Structure alignment can result in unused space, called padding. Padding between members of a structure is called internal padding. Padding between the last member and the end of the space occupied by the structure
is called tail padding. Figure 13.1, “Internal Padding in a Structure,” on page 157
, illustrates structure alignment. Consider the following structure: struct strc1 {
char a; /* occupies byte 0 */
short b; /* occupies bytes 2 and 3 */
char c; /* occupies byte 4 */
int d; /* occupies bytes 8 through 11 */
};
Figure 13.1. Internal Padding in a Structure
Figure 13.2, “Tail Padding in a Structure,” on page 158 , shows how tail padding is applied to a structure aligned on a doubleword (8 byte) boundary.
struct strc2{
int m1[4]; /* occupies bytes
0 through 15 */
double m2; /* occupies bytes 16 through 23 */
short m3; /* occupies bytes 24 and 25 */
} st;
Bit-field Alignment
Bit-fields have the same size and alignment rules as other aggregates, with several additions to these rules:
157
C and C++ Data Types
• Bit-fields are allocated from right to left.
• A bit-field must entirely reside in a storage unit appropriate for its type. Bit-fields never cross unit boundaries.
• Bit-fields may share a storage unit with other structure/union members, including members that are not bitfields.
• Unnamed bit-field's types do not affect the alignment of a structure or union.
• Items of [signed/unsigned] long long type may not appear in field declarations on 32-bit systems.
Figure 13.2. Tail Padding in a Structure
Other Type Keywords in C and C++
The void data type is neither a scalar nor an aggregate. You can use void or void* as the return type of a function to indicate the function does not return a value, or as a pointer to an unspecified data type, respectively.
The const and volatile type qualifiers do not in themselves define data types, but associate attributes with other types. Use const to specify that an identifier is a constant and is not to be changed. Use volatile to prevent optimization problems with data that can be changed from outside the program, such as memory#mapped I/O buffers.
158
Pakua
Tangazo