unsigned vs signed



  • Warum wird hier im Forum immer mit unsigned statt mit herumhantiert, die Benutzunt von size_t angepriesen etc.?
    Das führt nur zu so konstruiertem Code wie

    unsigned y_square = 1;
        unsigned x_square = 1;
        for (unsigned y = 1; y != root; ++y)
            for (unsigned x = 1; x != root; ++x)
    

    wo das "unsigned" rein gar kein Vorteil bringt.

    Hier mal ein paar Meinungen dazu:

    <a href= schrieb:

    Bjarne Stroustrup">The unsigned integer types are ideal for uses that treat storage as a bit array. Using an unsigned instead of an int to gain one more bit to represent positive integers is almost never a good idea. Attempts to ensure that some values are positive by declaring variables unsigned will typically be defeated by the implicit conversion rules.

    <a href= schrieb:

    http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml">Some people, including some textbook authors, recommend using unsigned types to represent numbers that are never negative. This is intended as a form of self-documentation. However, in C, the advantages of such documentation are outweighed by the real bugs it can introduce. Consider:

    for (unsigned int i = foo.Length()-1; i >= 0; --i) ...
    

    This code will never terminate! Sometimes gcc will notice this bug and warn you, but often it will not. Equally bad bugs can occur when comparing signed and unsigned variables. Basically, C's type-promotion scheme causes unsigned types to behave differently than one might expect.

    So, document that a variable is non-negative using assertions. Don't use an unsigned type.

    Hat das pushen "unsigned-correctness" in diesem Forum einen triftigen Grund oder wird das nur gebraucht um sich elitär zu fühlen? Ich sehe echt nicht, was das bringen soll. size_t zu verwenden hat bei immer mal wieder zu unerwarteten Bugs geführt und ich sehe echt keinen Vorteil davon.



  • signedup schrieb:

    Warum wird hier im Forum immer mit unsigned statt mit herumhantiert,

    Kannst du die Behauptung belegen?



  • Die Suche in diesem Forum ist extrem schlecht, aber diese Posts in den letzten Stunde gaben den Ausschlag:

    <a href= schrieb:

    Swordfish">[*]Der Datentyp für Speichergrößen ist size_t aus <stddef.h> , nicht int .

    <a href= schrieb:

    Arcoth">

    unsigned y_square = 1;
    	unsigned x_square = 1;
    
    	for (unsigned y = 1; y != root; ++y)
    	{
    		for (unsigned x = 1; x != root; ++x)
    		{
    


  • Also ich habs auch so gelert, dass man unsigned nimmt, wenn etwas nie negativ wird. Hab das eigentlich auch nie hinterfragt, weil es logisch klingt. Aber jetzt wo ich ein bissel gegoogelt hab, scheint das wohl nicht der best-practise weg zu sein. Dann steig ich wohl auf auto i=0; um 😃


  • Mod

    auto i=0;

    Och nö.



  • Arcoth schrieb:

    auto i=0;

    Och nö.

    Wieso?



  • signedup schrieb:

    Die Suche in diesem Forum ist extrem schlecht, aber diese Posts in den letzten Stunde gaben den Ausschlag:

    <a href= schrieb:

    Swordfish">[*]Der Datentyp für Speichergrößen ist size_t aus <stddef.h> , nicht int .

    <a href= schrieb:

    Arcoth">

    unsigned y_square = 1;
    	unsigned x_square = 1;
    
    	for (unsigned y = 1; y != root; ++y)
    	{
    		for (unsigned x = 1; x != root; ++x)
    		{
    

    Für immer ist das etwas dünn.
    Da in der STL an vielen Stellen size_t verwendet wird, muss man sich wohl oder Übel darauf einlassen, wenn man nicht mit Warnmeldungen zugeschmissen werden möchte.



  • <a href= schrieb:

    http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml">[...] Consider:

    for (unsigned int i = foo.Length()-1; i >= 0; --i) ...
    

    This code will never terminate![...]

    trwtf is not using iterators in the first place, doh!



  • signedup schrieb:

    <a href= schrieb:

    http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml">Some people, including some textbook authors, recommend using unsigned types to represent numbers that are never negative. This is intended as a form of self-documentation. However, in C, the advantages of such documentation are outweighed by the real bugs it can introduce. Consider:

    for (unsigned int i = foo.Length()-1; i >= 0; --i) ...
    

    This code will never terminate! Sometimes gcc will notice this bug and warn you, but often it will not. Equally bad bugs can occur when comparing signed and unsigned variables. Basically, C's type-promotion scheme causes unsigned types to behave differently than one might expect.

    So, document that a variable is non-negative using assertions. Don't use an unsigned type.

    Gezeigter Code ist kein Gegenargument, solche Fehler passieren allerhöchstens ein Mal und dann nie wieder. Gewünschtes Verhalten ist ebenso mit unsigned int zu ermöglichen:

    for(unsigned int i = foo.size(); i-- != 0; )
    

Anmelden zum Antworten