working with Array (basic code but very strange)

Hello

Why is the size of myArrayLast = 4.

// Declaration **
int myArrayActual[1];         // Array for all digital inputs
int myArrayLast[2] = {0,0};  // Array to compare with other array
void loop(){

  int a[17];
  int n = sizeof(a);
  Serial.print("n: ");
  Serial.println(n);

  Serial.print("myArrayActual: ");
  Serial.println(sizeof(myArrayActual));  
  Serial.print("myArrayLast: ");
  Serial.println(sizeof(myArrayLast));

SerialMonitor:
n: 34
myArrayActual: 2
myArrayLast: 4

It's always the double.

Thanks
Miller

Looks like returned size is in bytes, while the arrays are declared as ints.
1 int = 2 bytes

Ok.

What I have to do if I would this result

? myArray[ ? ] = {0,0};

sizeof(myArray()); // =2

Thanks

The statements below (which are data definitions, not declarations):

// Definition **
int myArrayActual[1];         // Array for all digital inputs
int myArrayLast[2] = {0,0};  // Array to compare with other array

asks for an array with one element, which is no different than a plain int, while the second definition asks for a two element array. The sizeof operator returns the total amount of memory allocated to the data item. Therefore, your first expression asking for the size of myArrayActual[] should return the size of a single int...2 bytes. The size of myArrayLast[] has two elements, so it returns 4 bytes.

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

So if you had the following declared:

int myArray[10];

The following would return 10:

ARRAY_SIZE(myArray)

(Thanks for this input: The statements below (which are data definitions, not declarations))

Ok, I understand the syntax of sizeof(arr()); now.

How can I find out the length of an array? I mean the content.

ex:

int arr[] = {0,0};
n = length of this array
Serial.print(n);

n = 2

Thanks.
Miller

Arrch has it correct. You are confusing the bytes allocated for the array with the number of elements in the array:

myArray[ ? ] = {0,0};

sizeof(myArray());        // =2

sizeof(myArray) return the bytes allocated to myArray[], which is 2 bytes for each element, or 4 bytes total. The macro Arrch showed you takes the memory allocated to the array (4 bytes) and divides it by the amount of memory needed for each element in the array (2 bytes) with the result being the element count for the array (2). Therefore:

long int biggerArray[] = {1,2,3,4,5,6,7,8,9,0};

using the macro would resolve to:

sizeof(biggerArray) / sizeof(biggerArray[0])
40 / 4
10 elements in the array, since each long requires 4 bytes of storage.

Thanks a lot for your very fast answers! :slight_smile:

I understand now.

That's the right way:

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

Greets from switzerland
Miller

asks for an array with one element, which is no different than a plain int,

It creates a storage location which contains one int and then it creates another variable which is a pointer to that location. Not the same at all.

michinyon:

asks for an array with one element, which is no different than a plain int,

It creates a storage location which contains one int and then it creates another variable which is a pointer to that location. Not the same at all.

I need to give up the gin - I can't make that out at all.

AWOL:

michinyon:

asks for an array with one element, which is no different than a plain int,

It creates a storage location which contains one int and then it creates another variable which is a pointer to that location. Not the same at all.

I need to give up the gin - I can't make that out at all.

He is saying int[0] != int because of how the code is compiled and values are stored in memory.

It creates a storage location which contains one int and then it creates another variable which is a pointer to that location

Nope, still can't see it..

Actually, I am wrong. I was thinking of classes. I have been working in .NET too much, thinking of all data types as derivative of Object. Plus I wrote int[0] instead of int[1], ha.

int i;
int i[1];

Those are the same. michinyon is incorrect. Both allocate 2 bytes of memory and add a pointer to the stack.

Anyways, off topic on what the OP asked.

I need to give up the gin - I can't make that out at all.

It's not the gin. Stone cold sober that statement is wrong.

Isn't all this a bit pointless in the small Arduino environment?

As the programmer you decide what size the array will be so why is there a need to use sizeof().
If you need to use the size of the array in a FOR loop, for example just use

const byte arrayLen = 6;
int myArray[arrayLen];

...R

As the programmer you decide what size the array will be

No, not always - you may have an automatically generated table that is simply an include file

const byte arbitraryLUT [] = {
#include "file.csv"
};

Robin2:
Isn't all this a bit pointless in the small Arduino environment?

Why does the size of the Arduino environment matter? It all gets optimized to the same values anyway.

Robin2:
As the programmer you decide what size the array will be so why is there a need to use sizeof().

So I need to update as little as possible if I need to make changes. Using the above mentioned macro scales very well with multiple arrays, particularly if the size of any of those arrays could change often between code uploads.

Isn't all this a bit pointless in the small Arduino environment?

As the programmer you decide what size the array will be so why is there a need to use sizeof().
If you need to use the size of the array in a FOR loop, for example just use

const byte arrayLen = 6;

int myArray[arrayLen];

I disagree strongly. The sizeof operator offers numerous advantages. Using the Array_Size() macro is typeless, so if I want to march through a for loop using an int array, I can use the macro. If I want to traverse a different array of type float, I can use the same macro because it is typeless. Your way requires defining a const for each array used in the program if they have different element counts. Also, if I change the size of the array, I need to remember to edit the length constant, too. The macro obviates such changes. Finally, the macro allows you to get away from "magic numbers" (e.g., 6) in the code.

Arrch:
Why does the size of the Arduino environment matter?

For the simple reason that it is so easy to run out of memory if you are careless about the size of arrays.

And dynamic memory allocation (I hope that is the correct term) does not work well in the Arduino.

...R

And dynamic memory allocation (I hope that is the correct term)

It isn't.

If you had dynamic memory allocation, you'd already know how much you'd asked for.
(And the environment would've told you if you couldn't have it)