int ledPins[] = {
2, 7, 4, 6, 5, 3 }; // an array of pin numbers to which LEDs are attached
int pinCount = 6; // the number of pins (i.e. the length of the array)
Is there a function that would set the pinCount integer to the number of variables in the ledPins array?
As far as I understand the compiler will calculate the pinCount during compiling and write the value to flash memory, i.e. the sizeOf() utility can not be used to calculate a value during run time?
KeithRB:
will set sp to the size of the int pointer, not the size of the malloc'd array. The same thing happens in function calls, since C is call by value.
That's because the type of p in your example is a pointer, not an array of known length. (That's also why it typically happens in functions - not because the value is copied, but because the type of the formal argument is an array of unspecified length - semantically equivalent to a pointer - rather than an array of known length.)
You cannot malloc() at compile time because you need to call a function, which only happens when the code starts running. Declaring an array like a[10] is how you allocate the memory at compile time.
KeithRB:
Right, how do you malloc an "array of known length" that would be known at run time vs compile time?
I don't recall ever doing it, but I imagine it'd be possible to do it indirectly (via a pointer) and declare the pointer as a pointer-to-array-of-specified-length. I suppose the length of the array would then be implied by the type of the pointer used to access it. It may be possible to clean up the use of pointers by using reference types. Alternatively, declare the array in a struct or as an instance field in a class that is created dynamically - you'd end up with an array of specified length which is allocated dynamically. From the point of view of knowing the array length, what matters is the type of the value, not where in memory it happens to reside or when it was allocated. In all these cases, the length of the array would be a compile time constant.
Sort of related, you can declare automatic array variables with lengths provided at runtime, but these are not allocated by malloc.
Unless you use 'malloc' or 'new' to create dynamic memory, this information is readily available by the compiler state. It knows the size of arrays even when there is no clear cut way to grab the info.
Here is a method that causes the compiler to provide the answer, rather than computing the value.
EDIT: The added bonus is it has protection against non array pointers, unlike the defines above.
I decided to try multidimensional arrays, but I'm getting some weird output from the Serial.print
int macros[][2] = {
(1,2,3,4,5,6),
(33,55,77)
};
void setup() {
Serial.begin(9600);
}
void loop() {
int Value01 = (macros[0][1]);
int Value02 = (macros[1][0]);
Serial.print("Value01 = ");
Serial.print(Value01);
Serial.println();
Serial.print("Value02 = ");
Serial.print(Value02);
Serial.println();
delay(1000);
}
I expected to get:
Value01 = 2
Value02 = 33
but instead I'm getting:
Value01 = 77
Value02 = starts with 0 and then goes through what a series of integers read from some memory location outside of the array.
pYro_65:
Here is a method that causes the compiler to provide the answer, rather than computing the value.
EDIT: The added bonus is it has protection against non array pointers, unlike the defines above.
template< typename T, size_t N > size_t ArraySize( T (&)[N] ){ return N; }
Yes, very cunning. Except it doesn't return a constant so it can't be used here:
template< typename T, size_t N > size_t ArraySize(T (&)[N] ){ return N; }
int foo [] = { 5, 6, 7, 8, 9 };
int bar [ ArraySize (foo) ]; // make another array the same size
Error:
sketch_mar12c:3: error: array bound is not an integer constant
This is becoming a very esoteric topic for what is usually a non-issue in code - either you know the size of the memory block at compile time (because it is a constant) or you allocate the memory by telling the libraries how much you want (in which case you know before you ask).
Getting back to basics - what are you trying to achieve?