Hello,
I am new to Arduino and I bought Arduino mega and Arduino Due. Recently when I run a program on these two boards, the results seem quite different, which may result from the difference in the memory size of variables. Where can I find a comparison of these aspects? e.g., is the memory size of type "long" the same in Due and Mega?
Thanks.
pert
February 8, 2018, 4:49am
2
You can determine it programmatically:
void setup() {
Serial.begin(9600);
while(!Serial){}
Serial.print("An int is ");
Serial.print(sizeof(int));
Serial.println(" bytes");
Serial.print("A long is ");
Serial.print(sizeof(long));
Serial.println(" bytes");
}
void loop() {}
On a DUE, an int or a float is 4 bytes long unlike on an 8_bit AVR. I find more handy to use these types:
uint8_t, uint16_t, uint32_t, uint64_t
int8_t, int16_t, int32_t, int64_t
plus of course boolean, float (4 bytes) and double( = double float, 8 bytes).
However, Serial is not capable to print all 64_bit variables on a DUE, you will need to use printf: here is an example sketch to do that on a DUE:
uint64_t bignum0 = 0xFFFFFFFFFFFFFFllu;
uint64_t bignum1 = (1llu<<64) - 1; //pow(2, 64) - 1;
int64_t bignum2 = -(1llu<<62); //- pow(2, 62);
double bignum3 = -1.12345678987654321;
double bignum4 = -166666666666666666666e-20;
void setup() {
Serial.begin(250000);
}
void loop() {
printf(" bignum0 = 0x%llx\n", bignum0);
printf(" bignum0 = 0x%llX\n", bignum0);
printf(" bignum1 = %llu\n", bignum1);
printf(" bignum2 = %lld\n", bignum2);
Serial.print(" bignum3 = ");
Serial.println(bignum3,15);
Serial.print(" bignum4 = ");
Serial.println(bignum4,16);
delay(1000);
}