bool ?



  • bool hat 1 BYTE.



  • Sqwan schrieb:

    also bool hat 1 bit nämlich 1 oder 0.
    char hat 1 Byte nämlich 8 bits
    ein int hat 2 Byte nämlich 16 Bits...
    und ein float hat 4Byte nämlich 32 Bits
    und ein double hat 8 byte. Nämlich 64 Bits...

    und auf einem anderen system. zB heutigen 32 bit x86er systemen sieht es komplett anders aus.



  • In C++ ist alles was addressierbar ist mindestens 1 Byte gross, daher auch bool. Und da jedes Byte in C++ per definitionem immer >= 8 Bitse haben muss ist das Verschwändung. Mir aber komplett Wurst 🙂
    Das "BOOL" von <windows.h> ist im übrigen ein typedef auf int, und auf "normalen" Maschinen daher 4 Byte gross.

    C++ erzwingt aber beim Schreiben dass in einem "bool" immer nur 0 oder 1 drinnen stehen kann (was auf BOOL ja nicht zutrifft). Solange man also nicht Pointer/Referenzen rumcastet und sich "deppert spielt" kann man davon ausgehen dass Tests wie "if(b == true)" sicher sind (was wieder auf BOOL nicht zutrifft).



  • Plotzenhotz: Der C++-Standard sagt überhaupt nix über die Byte-Größe.

    So, und nun die Lösung zu eurem Streitthema: Die Größe von bool hängt vom Zusammenhang und vielleicht sogar der Implementation ab :p



  • Michael E. schrieb:

    Plotzenhotz: Der C++-Standard sagt überhaupt nix über die Byte-Größe.

    Traurig aber wahr...
    Da wird immer über portablen Code gesprochen und dann kann man sich nichtmal darauf verlassen dass die Datentypen auf allen Systemen den gleichen Wertebereich abdecken.

    Gruß Spacelord



  • Spacelord schrieb:

    Michael E. schrieb:

    Plotzenhotz: Der C++-Standard sagt überhaupt nix über die Byte-Größe.

    Traurig aber wahr...
    Da wird immer über portablen Code gesprochen und dann kann man sich nichtmal darauf verlassen dass die Datentypen auf allen Systemen den gleichen Wertebereich abdecken.

    Gerade deshalb ist C++ auch auf so unterschiedlichen Systemen einsetzbar! Ne kleine Zusatzarbeit für den Programmierer ist es allerdings 😞



  • Äh.
    Blubb.
    Bin mir ziemlich sicher dass im C++ Standard (sinngemäss) steht dass ein Byte >= 8 Bit haben muss, die kleinste adressierbare Einheit der Maschine darstellt, und dass "char" genau ein solches Byte belegt.

    Steht dahier im übrigen auch so:
    http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.5

    Das bedeutet natürlich nicht dass das "echte" Byte der Maschine nicht < 8 Bit haben kann, bloss muss die C++ Implementierung das dann so "verstecken" dass das "C++ Byte" eben trotzdem wieder >= 8 Bit hat.

    Ich hab den Standard-Text leider nicht, sonst würde ich nachsehen.



  • Also laut meinem Buch hat bool 1 Byte, genau wie char...
    deshalb auch oft die Bitte "bool: Bitte ein bit"...*g*

    Typ Byte
    bool 1
    char 1
    short 2
    int 4
    float 4
    double 8

    Ich werde jetzt nur mehr true und false verwenden ohne typedef/define...
    Im Nachhinein betrachtet: Echt seltsam dass in irgendeinem uralten Header
    TRUE und FALSE definiert sind...Egal...
    Ich will mich nicht auf Zufälligkeiten verlassen müssen...
    Danke nochmal...stand echt auf der Leitung..*g*



  • Plotzenhotz schrieb:

    Äh.
    Blubb.
    Bin mir ziemlich sicher dass im C++ Standard (sinngemäss) steht dass ein Byte >= 8 Bit haben muss, die kleinste adressierbare Einheit der Maschine darstellt, und dass "char" genau ein solches Byte belegt.

    Steht dahier im übrigen auch so:
    http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.5

    Dann ist das wieder so eine von C geerbte Garantie (wie zum Beispiel auch die Mindestgröße von int).

    Kuldren schrieb:

    Im Nachhinein betrachtet: Echt seltsam dass in irgendeinem uralten Header
    TRUE und FALSE definiert sind...

    Nö, bool ist eben erst sehr spät als Built-in in die Sprache reingekommen.



  • Ich hab vom Standard nur nen Draft von 96.

    3.9.1 Fundamental types [basic.fundamental]
    1 Objects declared as characters (char) shall be large enough to store any member of the implementation’s
    basic character set. If a character from this set is stored in a character object, the integral value of that character
    object is equal to the value of the single character literal form of that character. It is implementationdefined
    whether a char object can hold negative values. Characters can be explicitly declared unsigned
    or signed. Plain char, signed char, and unsigned char are three distinct types. A char, a
    signed char, and an unsigned char occupy the same amount of storage and have the same alignment
    requirements (3.9); that is, they have the same object representation. For character types, all bits of
    the object representation participate in the value representation. For unsigned character types, all possible
    bit patterns of the value representation represent numbers. These requirements do not hold for other types.
    In any particular implementation, a plain char object can take on either the same values as a
    signed char or an unsigned char; which one is implementationdefined.

    2 There are four signed integer types: “signed char”, “short int”, “int”, and “long int.” In this
    list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural
    size suggested by the architecture of the execution environment37) ; the other signed integer types are
    provided to meet special needs.
    __________________
    37) that is, large enough to contain any value in the range of INT_MIN and INT_MAX, as defined in the header <climits>.

    3– 32 Basic concepts DRAFT: 2 December 1996 3.9.1 Fundamental types

    3 For each of the signed integer types, there exists a corresponding (but different) unsigned integer type:
    “unsigned char”, “unsigned short int”, “unsigned int”, and “unsigned long
    int,” each of which occupies the same amount of storage and has the same alignment requirements (3.9)
    as the corresponding signed integer type38) ; that is, each signed integer type has the same object representation
    as its corresponding unsigned integer type. The range of nonnegative values of a signed integer type
    is a subrange of the corresponding unsigned integer type, and the value representation of each corresponding
    signed/unsigned type shall be the same.

    4 Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number
    of bits in the value representation of that particular size of integer.39)

    5 Type wchar_t is a distinct type whose values can represent distinct codes for all members of the largest
    extended character set specified among the supported locales (22.1.1). Type wchar_t shall have the same
    size, signedness, and alignment requirements (1.6) as one of the other integral types, called its underlying
    type.

    6 Values of type bool are either true or false.40) [Note: there are no signed, unsigned, short, or
    long bool types or values. ] As described below, bool values behave as integral types. Values of type
    bool participate in integral promotions (4.5).

    7 Types bool, char, wchar_t, and the signed and unsigned integer types are collectively called integral
    types.41) A synonym for integral type is integer type. The representations of integral types shall define values
    by use of a pure binary numeration system.42) [Example: this International Standard permits 2’s complement,
    1’s complement and signed magnitude representations for integral types. ]

    8 There are three floating point types: float, double, and long double. The type double provides
    at least as much precision as float, and the type long double provides at least as much precision as
    double. The set of values of the type float is a subset of the set of values of the type double; the set
    of values of the type double is a subset of the set of values of the type long double. The value representation
    of floatingpoint
    types is implementationdefined.
    Integral and floating types are collectively
    called arithmetic types. Specializations of the standard template numeric_limits (18.2) shall specify
    the maximum and minimum values of each arithmetic type for an implementation.

    9 The void type has an empty set of values. The void type is an incomplete type that cannot be completed.
    It is used as the return type for functions that do not return a value. Any expression can be explicitly converted
    to type cv void (5.4). An expression of type void shall be used only as an expression statement
    (6.2), as an operand of a comma expression (5.18), or as a second or third operand of ?: (5.16).

    10 [Note: even if the implementation defines two or more basic types to have the same value representation,
    they are nevertheless different types. ]

    Zumindest da kann ich nirgendwo irgendwelche Garantien bezüglich Bytes und Bits entdecken.

    Gruß Spacelord



  • OK, hab jetzt nachgesehen, im Standard ist (für mich) nix zu finden was auf 8 Bit schliessen lässt. Ich vermute mal es ist aber vernünftig vorauszusetzen dass immer char >= 8 Bit, short & int >= 16 Bit und long >= 32 Bit. Auf diversen "exotischen" Plattformen die vielleicht ein native 7-Bit word haben wird man wohl eher nen C-Compiler antreffen als nen C++-Compiler, von daher denke ich kann man diese ignorieren 🙂

    Wie schön und einfach ist dagegen Java was das Thema angeht 😃



  • Kuldren schrieb:

    Also laut meinem Buch hat bool 1 Byte, genau wie char...

    Was char betrifft, ist das korrekt. Bei bool ist das allerdings nicht so. Der Standard legt lediglich fest, dass bool ein integraler Typ ist. Wenn ein Compiler also Lust hat, kann er bool intern auch auf long abbilden.

    Kuldren schrieb:

    Ich werde jetzt nur mehr true und false verwenden ohne typedef/define...

    Um's kurz zu machen, das ist auf jeden Fall die richtige Entscheidung. Und mehr gibt's dazu eigentlich auch nicht zu sagen.


Anmelden zum Antworten