Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 11 of 11

Thread: Is size of int type correct?

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Location
    Canada
    Posts
    16

    Is size of int type correct?

    I've been bashing my head for a bit, and I've discovered something confusing. I have been assuming the int type has a size of 2 bytes and I've discovered that it's 4. is this correct? Here is a small program showing the head scratcher. Normally, short and int have the same 2 byte size.

    Comments?

    Code:
    /*
    Teensy 3.5 Output
    Show Integer and Float Data Sizes
    
    long long is 8
    long is .... 4
    int is ..... 4 <-- I expected 2, why 4?
    short is ... 2
    char is .... 1
    double is .. 8
    float is ... 4
    */
    void setup()
    {
      Serial.println("Show Integer and Float Data Sizes\n");
    
      Serial.print("long long is "); Serial.println( sizeof(long long) );
      Serial.print("long is .... "); Serial.println( sizeof(long) );
      Serial.print("int is ..... "); Serial.println( sizeof(int) );
      Serial.print("short is ... "); Serial.println( sizeof(short) );
      Serial.print("char is .... "); Serial.println( sizeof(char) );
      Serial.print("double is .. "); Serial.println( sizeof(double) );
      Serial.print("float is ... "); Serial.println( sizeof(float) );
    
    }
    
    void loop()
    {
     
    }

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    970
    4 is correct. The Teensy 3.5 is a 32-bit processor. So are the other Teensy 3.x

    Pete

  3. #3

  4. #4
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    4,765
    If size width is important, use int32_t, uint32_t, int16_t, uint16_t...

    also note, double is 8 bytes (and its not =float like on AVR)

  5. #5
    Junior Member
    Join Date
    Nov 2018
    Location
    Canada
    Posts
    16
    El_supremo and Frank,
    Thanks for getting back back to me so quickly. I knew the move to 32 bit was going to cause some conversion problems with my 8 bit libraries, but I was a little surprised at int. I've always assumed that signed and unsigned int were 2 bytes in size. As Frank's link has shown me, int is not 'typed' in stone. Most of my code that I'm converting/re-writing, unfortunately, uses signed int and unsigned int. Fortunately, not a big deal, just another learning experience in the move to Teensy :-)

    Thanks for the help,
    Bruce

  6. #6
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    4,765
    you can just replace your ints with int16_t or uint16_t, this way they remain the same

    edit: ...and it will still work for your 8-bit mcu
    edit2: sometimes, in rare cases,with teensy, int16 is a little bit slower than int32. dunno what gcc exactly does in these cases, until now it was not that important for me to look for the details in the generated assembly. perhaps it adds additional code for overflows

  7. #7
    Junior Member
    Join Date
    Nov 2018
    Location
    Canada
    Posts
    16
    Hi Frank,
    That's what I'm doing now. My mistake was relying on the use of the fundamental data types and not using those defined in stdint.h. Stupid decision!!!

  8. #8
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    4,765
    yeah, in cases, where the the size is not important (for example a loop-counter 0..10 or something like that) , use the native int - which, in theory is the fastest type for the cpu.

  9. #9
    Junior Member
    Join Date
    Nov 2018
    Location
    Canada
    Posts
    16
    Agreed. Actually, you've just unknowingly answered a mental question of mine. Logically, if long long is 8 bytes, and long is 4 bytes, then int should be 2 bytes, but it's 4; only short is two.. Your comment "native int ..." is the old metaphorical "facepalm". That's why it's 4 bytes. Thanks Frank, now I get it. I may be slow, but I get there eventually;-)

  10. #10
    Senior Member brtaylor's Avatar
    Join Date
    Mar 2016
    Location
    Portland, OR
    Posts
    416
    This is a really good resource for ARM data type questions:
    http://www.keil.com/support/man/docs...9125009502.htm

  11. #11
    Junior Member
    Join Date
    Nov 2018
    Location
    Canada
    Posts
    16
    brtaylor,
    Thank you for sending me to that resource. In reading the page you sent me to, as well as glancing at a few other pages, it seems to be a useful site and I've bookmarked it for a more thorough read.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •