Number of bytes in array

Hi there, I am calculating my total bytes for arrays in different data types, that confused me.

For example:
int myint [5]={1,2,3,4};
char buf[25];

Based on what I understand, each element in integer carries 2 bytes, so for the first line, the total bytes will be 4x(2) bytes or 5x(2)bytes? I was confused if the empty elements in array is required to be added.
And for second line, each element in char carries 1 bytes, so the total bytes will be 25bytes or 24 bytes? Even if the numbers of elements I assign later are not reaching 24?

Thanks for answering my ques in advanced!

sizeof (myint)

Prove things for yourself, read this:

Arrays cannot have “empty” elements, the fifth element of int myint [5]={1,2,3,4}; will just be initialized to zero, but it does exist, so the size will be 10 bytes*.

The size of char buf[25]; will be 25 bytes. If it's a local variable, the 25 elements will be uninitialized. If it's a global, they will be initialized to zero.

(*) on AVR at least, the size of int can be different on different platforms.

The number of elements is what you have in the array definition, so myInt as 5 elements of type int which each take 2 bytes on a uno. So you will have 10 bytes for this array. If you were to skip the 5 and let the compiler decide, it would be 4 int so 8 bytes

Same rule for the char array, you will have 25 bytes

As mentioned the sizeof operator can tell you how many bytes got allocated

Hi~ so if I am putting one element in the array [25] for integer type, how the remaining elements will be then? be initialized zero as well? Then the total bytes will be 50?

Yes, as mentioned whatever you define is the size. And it’s fixed, you can’t change it later on. Indexes run from 0 to that number minus 1 (as we started at 0)

Global and static variables are zero initialized, the local variables are not initialized for you so could be whatever garbage was in memory at the time (on the stack or heap depending how you allocated the memory)

depends, might not. to be sure memset them to 0 if you rely on them being 0

int array[25]; declares an array of 25 integer elements. The first element is array[0] and the 25th (last) element is array[24]. The total number of bytes occupied will be 25 × sizeof(int), or 50 bytes on AVR.

If you want to initialize all elements to zero, use int array[25] {};

ok, how about below one,

char buf[25];
then the array was assigned as "AB";
so it is considered as one element only, am I right?

but the total bytes will be 25 still.

Can you explain your logic there, please?

The first one will be 5x2 bytes, the second one 25x1 bytes.

Accessing elements however is done by counting from 0, so you access myint with index values 0 .. 4 and buf withe index values 0 .. 24.

As mentioned above, you can use sizeof() to determine the size of any array in bytes. You can use the below macro to determine the number of elements (not bytes) in any type of array.

#define NUMELEMENTS(x) (sizeof(x) / sizeof(x[0]))

And an example

Serial.println(NUMELEMENTS(myint));

// edit
typing too slow

char buf[5] = "AB";
// is equivalent to
char buf[5] = {'A', 'B', '\0'};
// is equivalent to
char buf[5] = {'A', 'B', '\0', '\0', '\0'}; // 5 elements in total

wohhh so AB are counted as 2 elements ?
Then how about integer side?
for example,

int myint[3];

myint="13";

It is counted as 1 element right?

Of course, they are two chars and this is what you have in your array…

Note that you can provide content at initialization time


char buf[5] = "AB";

But not this ways at a later stage.


char buf[5];
…
  // later in code
  buf[5] = "AB"; // this is NOT legit

Because I assume the different elements are to be separated by comma, like,
"AB,AK"

That's a string into an int.

That’s 5 chars (plus a trailing null char)

Read this

Arrays of ordinary character types (char,...),... can be initialized from ordinary string literals, optionally enclosed in braces. Successive characters of the string literal (which includes the implicit terminating null character) initialize the elements of the array. If the size of the array is specified and it is larger than the number of characters in the string literal, the remaining characters are zero-initialized.

https://en.cppreference.com/w/cpp/language/aggregate_initialization

No, the string literal "AB" has 3 elements: the characters A, B and a terminating null character \0. If you assign a three-character string to an array of 25 elements, the array will have 25 elements, the length of the original string is irrelevant, all “unused” elements of the array are simply set to null (zero).

The comma is inside of the double quotes, it's part of the string literal, it's the character ',' and doesn't have any syntactic meaning.

char array[6] = "AB,AK";
// is equivalent to 
char array[6] = {'A', 'B', ',', 'A', 'K', '\0'};