2019/07/11

Arduino Nano sizeof()

In case you were wondering, here is the output of sizeof() and other things for an Arduino Nano. What's interesting (to me) is that while the Atmega328p is an 8-bit MCU, sizeof(int)==2 and sizeof(char *)==2. In other words, the compiler is treating it as a 16-bit processor.

Welcome to sizeof!
sizeof(bool)=1
sizeof(char)=1
sizeof(int)=2
sizeof(short)=2
sizeof(long int)=4
sizeof(long long int)=8
sizeof(char*)=2
sizeof(String)=6
sizeof(int8_t)=1
sizeof(uint8_t)=1
sizeof(int16_t)=2
sizeof(uint16_t)=2
sizeof(int32_t)=4
sizeof(uint32_t)=4
sizeof(int64_t)=8
sizeof(uint64_t)=8
__INT_MAX__=7FFF
__WINT_MAX__=FFFF
SHRT_MAX=7FFF
USHRT_MAX=FFFF
INT_MAX=7FFF
UINT_MAX=FFFF
LONG_MAX=7FFFFFFF
ULONG_MAX=FFFFFFFF

Here's the code that produces the above.

#include <avr/power.h>
#include <avr/sleep.h>
#include <limits.h>

#define SO( i ) Serial.print( "sizeof(" #i ")=" ); Serial.println( sizeof(i) )
#define Max( i ) Serial.print(  #i "=" ); Serial.println( i, HEX )

void setup(void)
{
    Serial.begin( 9600 );
    Serial.println( "Welcome to sizeof!" );
    
    SO( bool );
    SO( char );
    SO( int );
    SO( short );
    SO( long int );
    SO( long long int );
    SO( char* );
    SO( String );

    SO( int8_t );
    SO( uint8_t );
    SO( int16_t );
    SO( uint16_t );
    SO( int32_t );
    SO( uint32_t );
    SO( int64_t );
    SO( uint64_t );

    Max( __INT_MAX__ );
    Max( __WINT_MAX__ );
    Max( SHRT_MAX );
    Max( USHRT_MAX );
    Max( INT_MAX );
    Max( UINT_MAX );
    Max( LONG_MAX );
    Max( ULONG_MAX );
#ifdef LLONG_MAX
    Max( LLONG_MAX );
#endif
#ifdef ULLONG_MAX
    Max( ULLONG_MAX );
#endif
/*    Max( LONG_LONG_MAX );
    Max( ULONG_LONG_MAX ); */
}

void loop(void)
{

    delay(1000);    // allow serial buffer to empty
    set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
    sleep_mode(); // here the device is actually put to sleep!!
}