Go Down

Topic: How do you check the Length of an Array? (Read 2 times) previous topic - next topic

PeterH


Looks like you need pedantic warnings:



I'm surprised that the compiler accepted that code.

I'm even more surprised what happens if you put a delay() in:
Code: [Select]

int i;

void setup()
{
  Serial.begin(9600);

  delay(50);
 
  i = millis();
  char global[i];   
  Serial.println(sizeof (global));

  int j = millis();
  char local[j];   
  Serial.println(sizeof (local));
}
void loop()
{
}


Quote from: sketch

49
52


Um, is it allowed to do that?
I only provide help via the forum - please do not contact me for private consultancy.

ajofscott

I'm suprised he had any RAM left after dimensioning an array to the return from millis().

AWOL

Why are you surprised?
Read the code again.
"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.

lloyddean

#18
Feb 05, 2012, 12:23 am Last Edit: Feb 05, 2012, 12:27 am by lloyddean Reason: 1
C++ allows the size of stack based arrays to be dynamically stated (within the limits of stack size), but not resized.  It is valid code.

westfw

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.
Code: [Select]
int myarray[15];
void loop() {
  operate(myarray);
}
void operate(int arr[])
{
  Serial.print(sizeof(arr));
}

Will print "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.

Go Up