Pages: [1] 2 3   Go Down
Author Topic: How do you check the Length of an Array?  (Read 1537 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3412
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Sr. Member
****
Karma: 1
Posts: 260
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46063
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Jr. Member
**
Karma: 0
Posts: 58
OS Developer, ASIC and FPGA designer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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";
array name is a address,
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
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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
Jump to: