5

I set out to see how much space some arrays have. I used this test code

bool state = false;

uint16_t BuffA[46000];
uint16_t BuffB[20000];
uint16_t BuffC[20000];
uint16_t all = 0;

void setup(){
Serial.begin(9600);
}

void loop(){
if (state==false){
Serial.println(sizeof(BuffA));
Serial.println(sizeof(BuffB));
Serial.println(sizeof(BuffC));
all = sizeof(BuffA) + sizeof(BuffB) + sizeof(BuffC)
Serial.println(all);
state = true;
  }
}

I got this answer from the serial:

92000
40000
40000
40928
  1. Why are the results doubled from those i initiated? Shouldn't the first one be 46kb and the other two 20kb?
  2. Why is the last value only 40928?
  3. The RAM of Due is 96kb. Does this mean that with the initialization of only three arrays it's almost full? Ifi crossed the RAM limit, would the IDE throw an error?
  4. If i still need more RAM, can i use and external one with Due for high speed RAM acquisition?
user1584421
  • 1,425
  • 3
  • 26
  • 36

3 Answers3

9

sizeof doesn't return the number of elements. It returns the number of bytes. Since they are uint16_t arrays each element is 2 bytes - hence twice the size.

The reason your last number is only 40928 is because of integer wraparound. You only provide a 16-bit unsigned variable to store it in, so all you get is the lower 16 bits of the answer.

92000 + 40000 + 40000 = 172000

172000 in hex is 0x29FE0

0x29FE0 truncated to 16 bits is 0x9FE0

0x9FE0 in decimal is 40928.

The RAM of Due is 96kb. Does this mean that with the initialization of only three arrays it's almost full? Ifi crossed the RAM limit, would the IDE throw an error?

It would, but you aren't actually using the arrays for anything, so the compiler threw them away.

If i still need more RAM, can i use and external one with Due for high speed RAM acquisition?

There are ways of interfacing the SAM3X with external RAM, but I don't thing the Due exposes all the signals you need. I would change to a chip with more RAM, such as the PIC32MZ with 512kB of RAM and 200MHz operation.

Majenko
  • 105,851
  • 5
  • 82
  • 139
4

The sizeof operator's result is number of bytes, not number of array elements. Your arrays in the program above have two bytes per element, hence twice as many bytes as elements.

If you want to report the number of elements, divide the total size by the number of bytes per element. For example:

nElements = sizeof BuffA /  sizeof BuffA[0];

As uint16_t variables have 16 bits, the total all will exceed the maximum (65535) that all can hold. Consider using uint32_t type for that variable. For example:

uint32_t all = sizeof BuffA/sizeof BuffA[0] + sizeof BuffB/sizeof BuffB[0] + sizeof BuffC/sizeof BuffC[0];

Because the type of sizeof is size_t, a “type able to represent the size of any object in bytes”, one need not cast any of addends to uint32_t for the result to not overflow.

James Waldby - jwpat7
  • 8,920
  • 3
  • 21
  • 33
1
  1. A variable of uint16_t has 16 bits, thus two bytes, so for e.g. BuffA[46000] this means 46000 * 2 = 92000 bytes.

  2. The variable all is also 16 bits and can contain only as max value 65,535. 92000 + 40000 + 40000 = 172000 bytes but that does not fit in 16 bits. Therefore the value is clipped ... make it a uint32_t variable.

  3. The total is 172.000 bytes, this mean it will not fit in 96KB. What are the value ranges you want to store? If the values are max 255

  4. You can use extra RAM. DRAM is very hard to use, EEPROM is only usable when you don't have to write it too often. Otherwise use SRAM, e.g. the 23LC1024's which can store 128 KB RAM and are only DIP8.

Michel Keijzers
  • 13,014
  • 7
  • 41
  • 58