1.13. C++ Simple Types

[ fromfile: cppintro.xml id: simpletypes ]

Table 1.2. Simple Types Hierarchy

byte/char TypesIntegral Types Floating Point Types
bool short int float
char unsigned short double
signed char intlong double
unsigned char unsigned int  
wchar_t anyType*  
 long int  
  unsigned long  

C++ Object Size

Example 1.18. 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;                                       1
    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";     2
    cout << "sizeof(qint64) = " << sizeof(qint64) << '\n';      3
    cout << "sizeof(QChar) = " << sizeof (QChar) << endl;       4
    cout << "sizeof(QDate) = " << sizeof(QDate) << endl; 
    cout << "qstring.length() = " << qstring.length() << endl;  5
    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


1

Pointer to first element of array.

2

Guaranteed to be 32 bits on all platforms.

3

Guaranteed to be 64 bits on all platforms.

4

Twice as big as a char

5

For # of bytes, be sure to take into account the size of QChar.


[Note] 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.



[8] For further discussion of the differences between signed an unsigned integral types, see Section 19.5.