badonj
December 20, 2024, 11:47pm
1
I decided to post this as I could not find and example for Arduino. Just post that did not explain it well that are locked. Here is the code that works.
Example I made for NeoPixel Project:
//int length = sizeof(numbers) / sizeof(numbers[0]);
volatile uint32_t WHITE = strip1.Color(255, 255, 255);
volatile uint32_t ORANGE = strip1.Color(255, 20, 0);
volatile uint32_t RED = strip1.Color(255, 0, 0);
volatile uint32_t GREEN = strip1.Color(0, 255, 0);
volatile uint32_t BLUE = strip1.Color(0, 0, 255);
volatile uint32_t YELLOW = strip1.Color(255, 223, 0);
volatile uint32_t PURPLE = strip1.Color(82, 2, 71);
volatile uint32_t uiColors[] = {RED, GREEN, BLUE, YELLOW, PURPLE};
Serial.print("Length of Colors Array: ");
Serial.println(sizeof(uiColors) / sizeof(uiColors[0]));
/*
Serial Monitor returns:
Length of Colors Array: 5
*/
Source to c++ example explaining it:
AI Overview from Google Search of "array length c++"
To get the length of an array in C++, you can use the following method:
C++
#include <iostream>int main() { int numbers[] = {10, 20, 30, 40}; // Calculate the length of the array int length = sizeof(numbers) / sizeof(numbers[0]); std::cout << "Length of the array: " << length << std::endl; return 0;}
Explanation:
sizeof(numbers): This returns the total size of the array in bytes.
sizeof(numbers[0]): This returns the size of the first element in the array in bytes.
Division : Dividing the total size by the size of a single element gives you the number of elements in the array.
Important Points:
This method only works for arrays that are declared as static arrays (like the example above).
If you are working with dynamic arrays created using new, you need to keep track of the length yourself.
For C++ standard library containers like std::vector, you can use the size() member function to get the length directly.
Generative AI is experimental.
b707
December 21, 2024, 12:00am
2
The code contains a lot of extra lines.
All what you need are array declaration and printing the size.
As it will always be.
The ‘Generational ’ in the name identifies it. as evolving.
BTW, for GPT, it’s
Generational Pre-Training.
An aggregation of most likely outcomes from preceding queries… not the most accurate.
1 Like
The better way in my opinion
#define NUMELEMENTS(x) (sizeof(x) / sizeof(x[0]))
uint8_t byteArray[5];
int16_t intArray[5];
void setup()
{
Serial.begin(115200);
Serial.print(F("Elements in byteArray = "));
Serial.println(NUMELEMENTS(byteArray));
Serial.print(F("Elements in intArray = "));
Serial.println(NUMELEMENTS(intArray));
}
void loop()
{
}
1 Like
badonj
December 21, 2024, 4:46am
5
It is for a dynamic function that is not obvious from the code I shared. It was the only way to do the function I am doing. If not needed, then less code could be used.
badonj
December 21, 2024, 4:48am
6
Thanks, will try that out. less math to do over and over again. will speed up code execution.
noiasca
December 21, 2024, 7:38am
7
badonj:
volatile uint32_t PURPLE = strip1.Color(82, 2, 71);
volatile uint32_t uiColors[] = {RED, GREEN, BLUE, YELLOW, PURPLE};
I just wonder why you want to use the volatile keyword and put a copy of each value in an additional array. As colors won't change so much a const or constexpr makes far more sense for me.
J-M-L
December 21, 2024, 8:07am
8
The use of static or declared here is somewhat misleading. You would have to further define that.
1 Like
gcjr
December 21, 2024, 11:41am
9
the size of the array has to be known at compile time for sizeof() to work
J-M-L
December 21, 2024, 12:04pm
12
Try this
void setup() {
Serial.begin(115200);
size_t n = random(255) + 1;
Serial.println(n);
int vla[n] = {42};
Serial.println(sizeof vla);
}
void loop() {}
You should see two numbers, the second being twice or 4 times the first one depending on the size of an int on your arduino.
gcjr
December 21, 2024, 12:23pm
13
i tried following, get different values depending on pot setting
looks like sizeof() is a run-time function
thanks for making me aware of this
void setup() {
Serial.begin(115200);
}
void loop () {
size_t n = analogRead (A0);
Serial.print(n);
Serial.print(" ");
byte vla[n];
Serial.println(sizeof vla);
delay (100);
}
sizeof() can be either compile-time or run-time, depending on whether the compiler has enough information to evaluate the value at compile-time.
alto777
December 21, 2024, 12:38pm
15
sizeof is an operator, when we talk about it there is no need for parentheses, as there is no need for parentheses in many common uses.
Similar in a way to return , while I am thinking about it. No one writes "return()" when talking about it.
Since my lifetime supply of ink is running low, and my vision is equally suffering from age, I prefer to have fewer characters and more white space.
const byte myPins[2, 3, 4, 5, 6];
const byte nPins = sizeof myPins / sizeof *myPins;
I understand that parentheses are essential in macros and things like that.
I also omit { braces } when they do nothing for the compiled code, and I pay the price occasionally.
a7
gfvalvo
December 21, 2024, 1:29pm
16
Interesting. If sizeof is truly functioning at runtime, where is the array's size information stored?
J-M-L
December 21, 2024, 2:01pm
17
VLAs are not part of the standard but are supported as an extension by some compilers like GCC.
If I remember well, the size of a VLA (ie which is determined at runtime) is stored in a hidden stack variable (typically stored on the stack alongside the VLA itself) and when sizeof is used against the VLA, the compiler generates code to retrieve this size from the hidden information.
J-M-L
December 21, 2024, 2:04pm
18
You need them on a type though like sizeof(int)
J-M-L
December 21, 2024, 2:07pm
19
Outside of VLAs, sizeof is always a compile-time operator in standard C++, because the size of all other objects and types is known at compile time (and for VLAs it’s just a read of a variable on the stack)
alto777
December 21, 2024, 2:26pm
20
J-M-L:
You need them on a type
I just saw that it my well worn copy of K&R, it caught me by surprise. I then threw one at an online compiler to learn and lock it in.
... main.c:11:18: error: expected expression before ‘int’
11 | int wtf = sizeof int;
I'm sure there is some good reason for that.
a7