[ fromfile: cppintro.xml id: simpletypes ]
The simple types supported in C/C++ are listed in Table 1.2.
C/C++ also provides a special symbol that signifies the absence of type information: void
.
C++ simple types can (variously) be modified by the following keywords to produce other simple types.
short
long
signed
unsigned
[14]
Table 1.2. Simple Types Hierarchy
byte/char Types | Integral Types | Floating Point Types |
---|---|---|
bool | short int | float |
char | unsigned short | double |
signed char | int | long double |
unsigned char | unsigned int | |
wchar_t | anyType* | |
long int | ||
unsigned long |
C++ compilers allow you to omit "int
" from the type names short int
, long int
, and unsigned int
.
You can also omit signed
from most types because that is the default.
Object size in C++ is expressed in terms of the size of char
.
In this metric,
the size of char
is 1.
Because the range of values for a particular type depends on the underlying architecture of the compiler's platform,
the ANSI/ISO standard for C++ does not specify the size of any of the types displayed in Table 1.2.
It guarantees only that a given type (e.g., int
) must not be smaller than one that appears above it (e.g., short
) in the table.
There is a special operator sizeof()
that returns the number of char
-sized memory cells that a given expression requires for storage. On most systems, a single char
is stored as an 8-bit byte.
Unlike most functions, the sizeof()
operator can take arguments that are value expressions or type expressions.
Example 1.20 shows how sizeof()
can be used and some of the values it returns on a 32-bit x86 system.
Example 1.20. src/early-examples/size/qsize.cpp
#include <QString> #include <QTextStream> #include <QChar> #include <QDate> int main() { QTextStream cout(stdout); char array1[34] = "This is a dreaded C array of char"; char array2[] = "if not for main, we could avoid it entirely."; char* charp = array1; QString qstring = "This is a unicode QString. Much preferred." ; Q_ASSERT (sizeof(i) == sizeof(int)); cout << " c type sizes: \n"; cout << "sizeof(char) = " << sizeof(char) << '\n'; cout << "sizeof(wchar_t) = " << sizeof(wchar_t) << '\n'; cout << "sizeof(int) = " << sizeof(int) << '\n'; cout << "sizeof(long) = " << sizeof(long) << '\n'; cout << "sizeof(float) = " << sizeof(float) << '\n'; cout << "sizeof(double) = " << sizeof(double) << '\n'; cout << "sizeof(double*) = " << sizeof(double*) << '\n'; cout << "sizeof(array1) = " << sizeof(array1) << '\n'; cout << "sizeof(array2) = " << sizeof(array2) << '\n'; cout << "sizeof(char*) = " << sizeof(charp) << endl; cout << " qt type sizes: \n"; cout << "sizeof(QString) = " << sizeof(QString) << endl; cout << "sizeof(qint32) = " << sizeof (qint32) << "\n"; cout << "sizeof(qint64) = " << sizeof(qint64) << '\n'; cout << "sizeof(QChar) = " << sizeof (QChar) << endl; cout << "sizeof(QDate) = " << sizeof(QDate) << endl; cout << "qstring.length() = " << qstring.length() << endl; return 0; } Output: (example run on 32-bit system) sizeof(char) = 1 sizeof(wchar_t) = 4 sizeof(int) = 4 sizeof(long) = 4 sizeof(float) = 4 sizeof(double) = 8 sizeof(double*) = 4 sizeof(array1) = 34 sizeof(array2) = 45 sizeof(char*) = 4 qt type sizes: sizeof(QString) = 4 sizeof(qint32) = 4 sizeof(qint64) = 8 sizeof(QChar) = 2 sizeof(QDate) = 4 qstring.length() = 42
<include src="src/early-examples/size/qsize.cpp" href="src/early-examples/size/qsize.cpp" id="sizecpp" mode="cpp"/>
Notice that all pointers are the same size, regardless of their type.
In the output, you can see that sizeof(qstring)
is only 4 bytes, but it is a complex class that uses dynamic memory, so you must call length()
to get the number of QChar in the string.
Because a QChar is twice the sizeof(char), you need to double the length to compute the actual size of qstring in memory.
Under the covers, QString can share memory with other strings that have the same value, so after a copy, the dynamic memory does not "belong" exclusively to one QString object.
The ranges of values for the integral types (bool, char, int
) are defined in the standard header file limits.h
.
On a typical *nix installation that file can be found in a subdirectory of /usr/include
.
Initialization of Basic-Typed Variables | |
---|---|
When a variable is of a basic type, it must be initialized! Uninitialized variables of basic types have undefined values on program startup. They may have initial value 0 when running in a debugger and have garbage values when run in other envrionments. |
[14] For further discussion of the differences between signed an unsigned integral types, see Section 19.5.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |