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
int ledPins[] = {2,3,4,5,6,7,8,9}
Then in the set program it suggests to loop through them like this
void setup()
{
for(int i = 0; i < 8; i++)
{
pinMode(ledPins[i], OUTPUT);
}
}
Whereas I, in PHP would done something like
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.
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.
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.
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.
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.
wh33t:
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:
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.
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
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
Note, by the way, that you can not use the "sizeof" operator on arrays that have been passed to a function, since arrays are always passed by reference (a pointer), and the size of a pointer is always 2.
Yes, there are reasons to support things like dynamically sized arrays, and arrays that contain information about their actual size. But the C language doesn't support either one; it's a "primitive" language.