Pages: [1] 2 3   Go Down
 Author Topic: How do you check the Length of an Array?  (Read 801 times) 0 Members and 1 Guest are viewing this topic.
Offline
Jr. Member
Karma: 0
Posts: 55
 « on: February 04, 2012, 05:02:16 am » Bigger Smaller Reset

I was just curious if there was a way to check the length of an array?

I didn't see any direct answers to this on the forums and the Array function page on the website didn't say anything about checking the length either.

I was hoping to see something like sizeof(array_name), but sizeof() doesn't work that way with Arduino does it?
 Logged

Gosport, UK
Offline
Karma: 19
Posts: 3117
 « Reply #1 on: February 04, 2012, 05:09:24 am » Bigger Smaller Reset

Usually, you will have created the array, so you'll know the length of it.
 Logged

Offline
Jr. Member
Karma: 0
Posts: 55
 « Reply #2 on: February 04, 2012, 05:14:29 am » Bigger Smaller Reset

So you must always keep track of it's length with another variable then?
 Logged

Gosport, UK
Offline
Karma: 19
Posts: 3117
 « Reply #3 on: February 04, 2012, 05:26:31 am » Bigger Smaller Reset

What exactly do you mean by length. Arrays are fixed at the length you define them.
 Logged

Offline
Jr. Member
Karma: 0
Posts: 55
 « Reply #4 on: February 04, 2012, 05:43:36 am » Bigger Smaller Reset

Would there not ever be a time where you would want to add things into an array during Run Time?

I'm still just starting out with Arduino but I have a php programming background.

I'm going through this lesson booklet I have and the one I'm currently on was to set up 8 blinking LEDs.
As the lesson book suggested I stored them in an array

Code:
int ledPins[] = {2,3,4,5,6,7,8,9}

Then in the set program it suggests to loop through them like this

Code:
void setup()
{
for(int i = 0; i < 8; i++)
{
pinMode(ledPins[i], OUTPUT);
}
}

Whereas I, in PHP would done something like

Code:
void setup()
{
for(int i = 0; i < sizeof(ledPins); i++)
{
pinMode(ledPins[i], OUTPUT);
}
}

Like I said, I'm still starting out and just trying to get used to the language so I'm not actually sure if there would ever be a case where an Array might grow beyond it's declared size during run time but I figured this would be common.
 Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
 « Reply #5 on: February 04, 2012, 05:56:14 am » Bigger Smaller Reset

sizeof(ledPins)  is 16, so you wouldn't want to use that for your loop control.
 Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

New Jersey
Online
Edison Member
Karma: 24
Posts: 2353
 « Reply #6 on: February 04, 2012, 06:04:23 am » Bigger Smaller Reset

Arrays in C are just a syntactic convenience to make it easier to use a piece of contiguous memory. As such, arrays don't have any attributes that you can use to tell you how big they are at run time. You can indeed use sizeof at compile time on a static array, but if you've used memory allocation to create it, you have to know how big it is - you can't find out after the fact.
 Logged

Offline
Sr. Member
Karma: 1
Posts: 260
 « Reply #7 on: February 04, 2012, 06:06:08 am » Bigger Smaller Reset

sizeof() tells you how many memory is reserved for a datatype, variable or array.

sizeof(int) = size of memory reserved for an integer-value.

sizeof(my_array) = not the number of values the array stores, but the complete size of the array in memory.

If you have an integer-array, you can take the complete array-memory-space which is reserved for the array and divide it by the size of an integer-values.

Code:
int myarray[] = {123, 456, 789}; // 3 values stored
int number_of_stored_values_in_myarray = sizeof(myarray)/sizeof(int);
if(number_of_stored_values_in_myarray == 3)
Serial.println("The Array stores 3 values");

Do you understand what i mean?
 « Last Edit: February 04, 2012, 06:14:54 am by kduin » Logged

Gosport, UK
Offline
Karma: 19
Posts: 3117
 « Reply #8 on: February 04, 2012, 06:45:31 am » Bigger Smaller Reset

Quote
if there would ever be a case where an Array might grow beyond it's declared size during run time

Array sizes are fixed at compile time. If you try to go beyond the declared size, you will be overwriting the next object's memory. It is your responsibility to keep track.
 Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
 « Reply #9 on: February 04, 2012, 06:56:14 am » Bigger Smaller Reset

Quote
Array sizes are fixed at compile time
Apart from the ones you've malloced.

There is a serious point to make here, and it is that the Arduino architecture is very memory-constrained, and you must be mindful of this.
 Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

Seattle, WA USA
Offline
Brattain Member
Karma: 315
Posts: 35519
Seattle, WA USA
 « Reply #10 on: February 04, 2012, 08:53:34 am » Bigger Smaller Reset

To determine the number of elements in an array, you can indeed use the sizeof() function, but, you need to use it twice.

const int arrLen = sizeof(array) / sizeof(array[0]);

The first call will tell you how much memory the whole array is using. The second will tell you how much memory one element is using. The division then tells you how many elements in the array.

Keep in mind that this method can not be used in functions, since the function receives a pointer to an array, so it can not possibly determine the size of the array.

If you are using malloc to change the size of an array, you know how big the array is.
 Logged

Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
 « Reply #11 on: February 04, 2012, 11:24:21 am » Bigger Smaller Reset

I was just curious if there was a way to check the length of an array?

I don't disagree with anything said above, but you can do this in effect by using STL (standard template library). There is a version for Arduino:

http://andybrown.me.uk/ws/2011/01/15/the-standard-template-library-stl-for-avr-with-c-streams/

There are overheads, of course. The library uses some program memory, and allocating arrays dynamically will use RAM.

With only 2 Kb or so of memory for most Arduino processors you are probably better off designing with fixed lengths in mind, but if the occasion warrants it, dynamic memory allocation, with or without the STL, can help.
 Logged

India
Offline
Jr. Member
Karma: 0
Posts: 58
OS Developer, ASIC and FPGA designer
 « Reply #12 on: February 04, 2012, 12:37:32 pm » Bigger Smaller Reset

sizeof() is not a function, it s a operator,
when you define a array.. i.e.  cha array[10];
theoritically  the memory should allocate in compile time
no resize will possible, if arduino support this feature then it is not a c,C++ standard.
sizeof operator always returns no's in byte.

char *ptr="hello";
char array[]="hello";
where as ptr points to a address, and ptr can also point to other location i.e. ptr= array;
but array name is a address but you can't change it.

I observed arduino supports
-------------------------------------------
Code:
int i;
:
:
i=getsomeVal();
char array[i]; //<-------Synatically its wrong and arduino also failed to allocate the memory
// As we know  in C,C++ char array[constant exp] at declarative statement
------------------------------

 « Last Edit: February 04, 2012, 12:42:39 pm by pravas » Logged

Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
 « Reply #13 on: February 04, 2012, 03:20:54 pm » Bigger Smaller Reset

That's odd:

Code:
int i;

void setup ()
{
i =  millis () ;
char array[i];
Serial.begin (115200);
Serial.println (sizeof (array));
}
void loop () {}

That outputs "0".

But it isn't just Arduino. g++ does it. For example g++ 4.2 on the Mac:

Code:
int i;
volatile int j;

int millis ()
{
return j;
}

int main ()
{
i =  millis () ;
char array[i];
}

Compiles without errors.
 Logged

Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
 « Reply #14 on: February 04, 2012, 03:25:24 pm » Bigger Smaller Reset

Looks like you need pedantic warnings:

Code:
\$ g++ -Wall -pedantic test.cpp

test.cpp: In function ‘int main()’:
test.cpp:12: error: ISO C++ forbids variable-size array ‘array’
 Logged

 Pages: [1] 2 3   Go Up