What is "sizeof (int)"

Really basic question: in the following lines, what is being indicated by "sizeof (int)"?

The Sketch:
` const int VOL = A0; // Volume Pot pin
const int HALLPIN1 = A1; // hall effect analog sensor pins 1-5
const int HALLPIN2 = A2;
const int HALLPIN3 = A3;
const int HALLPIN4 = A4;
const int HALLPIN5 = A5;
const int leds[4] = {5,6,10,11};

void setup() {

pinMode(A0, INPUT); //volume
pinMode(A1, INPUT_PULLUP); //analog Ch 1
pinMode(A2, INPUT_PULLUP); //analog Ch 2
pinMode(A3, INPUT_PULLUP); //analog Ch 3
pinMode(A4, INPUT); //analog Ch 4
pinMode(A5, INPUT); //analog Ch 5

for (int jj; jj<sizeof(leds)/sizeof(int);jj++) // this sets individual LEDs for randFlash
{
pinMode(leds[jj],OUTPUT);
delay(9);
}
}
void randFlash()
{
digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],HIGH);
delay(random(4,57));
digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],LOW);
}
void noFlash()
{
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
}
void ledOff()
{
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
void loop() {

int volPotVal = analogRead(VOL);
int sens1Val = analogRead(HALLPIN1);
int sens2Val = analogRead(HALLPIN2);
int sens3Val = analogRead(HALLPIN3);
int sens4Val = analogRead(HALLPIN4);
int sens5Val = analogRead(HALLPIN5);

if ((sens1Val < 400) // Tests if any hall sensor value is under 400
|| (sens2Val < 400)
|| (sens3Val < 400)
|| (sens4Val < 400)
|| (sens5Val < 400))
{randFlash(); // randomly flashes LEDS
}
else {noFlash(); // LEDs solidly lit
}
}
`
These bits of code work, and I'm trying to parse it out. I understand MOST of what's happening here, but I don't know how sizeof(leds)/sizeof(int) works.

sizeof(leds) returns a value of 4, right? The 4 leds in my array? is it right that 4 is being divided by whatever the value of sizeof(int) is?

I'm in the Getting Started with Sketches book, but I haven't seen how this works based on the examples given therein. Can someone help me parse this so I can move on?
Thanks...

sizeof() - Arduino Reference

On UNO and other 8-biters, an int is 16 bits. On STM/ESP 32-bit uC, the int is 32-bits. 16-bits = 2 bytes and 32 bits = 4 bytes.

1 Like

Welcome! It makes it much easier if you post when needed a schematic of the wiring, not a frizzy picture. Post a clear photo of the wiring can always help. Post the code in code tags. Explain what should the project do? Then tell us what it actually does? Do you get compile errors, If so, post the errors. The size of an integer varies depending on the particular computer/processor. Current processors range from 4 bits to at least 64 bits. There was even a 1 Bit by Motorola. Generally it is the size of the general purpose register sometimes called accumulator, varies by machine, that the size is based on. Code can be constructed in such a way that an int can be 16 bits regardless of the computer, this happens many times with compilers for 8 bit machines. Machines like the 8080 etc would combine two inter 8 bit registers (H-L or D_E for example) to get an actual 16 bit register.

1 Like

Ahh, yes.
This is running on an Uno. I don't have the schematic drawn out, unfortunately. This is an old radio with a needle dial atop which is mounted a small magnet. The magnet passes under the 5 analog Hall Effect sensors to trigger analog inputs (HALLPIN1-5). When triggered (sensxVal < 400 - a "station"), the randFlash() function is run, causing the 5 leds to flicker randomly. When the dial is in between stations (no HE sensors triggered), the leds are lit solid (noFlash function).

It all works; I'm just trying to figure out why! I don't fully understand the condition part of that for statement, or how it interacts with similar conditions in the digitalWrite commands of the randFlash function..

It sort of looks like this, but I'm not using a Feather breakout, as the sketch offered by the original designer wouldn't compile, and the sound board he specified turns out to have huge problems. I don't have any idea how the designer got it to work with the parts he claims to have used. Wasted a lot of money, then went back to things I know a little bit better, Arduino Uno and a Wav Trigger board for sound.

Here's a link to the original project:

Your switch appears to do nothing. I have no clue as to what the parts you are using.

The hardware doesn't matter here... It works and my question is about a specific couple of lines of code, not the hardware triggering circuit.

Thanks. It's an Uno so I guess the value being returned is 16..? Still not understanding the function of the for condition...

sizeof (leds) is how many bytes the leds array consumes.

sizeof (int) is how many bytes it takes to hold an int.

Since leds is an array of ints, the calculation results in how many elements there are in the leds array.

So you don't have to worry about chasing around all the places you need to use that number, the number of elements in the array.

Then this for loop

 for (int jj; jj<sizeof(leds)/sizeof(int);jj++) {

woukd run jj over the entire array.

But it really should be

for (int jj = 0; jj<sizeof(leds)/sizeof(int); jj++)  {

Not sure how you've been getting away with not initialising the index variable jj

a7

1 Like

This is the original sketch I found online and adapted to my purposes:

int leds[6] = {8,9,10,11,12,13};

void setup(){

for (int jj; jj<sizeof(leds)/sizeof(int);jj++){

pinMode(leds[jj],OUTPUT);

delay(10);

}

}

void loop(){

digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],HIGH);

delay(random(20,200));

digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],LOW);

}

Thanks for the insight. I'm a fairly new coder, and there's a lot of stuff I don't yet undeerstand, so I appreciate your time in explaining!

Find another sketch :slight_smile:

for (int jj; jj<sizeof(leds)/sizeof(int);jj++){

It would be a lot easier of you don't use sizeof(int) in there but sizeof(leds[0]. If you decide to make the leds array an array of bytes instead of integers, you don't have to adjust the for-loop.

And as pointed out, jj is not initialised. So the complete line would be

for (int jj = 0; jj<sizeof(leds)/sizeof(leds[0]);jj++){

No, it will be 2, the number of bytes; not the number of bits.

PS
Please use code tags in future as described in How to get the best out of this forum. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

And learn how to use code tags when posting code. Makes it easier for people to help. The tags are here :point_up_2:when you are posting or editing and look like this <|>. You can edit posts to add them using the :pencil2: :point_down:

based on what @alto777 and @sterretje already explained I would like to introduce you to the auto range base for loop. It's something like you have in other languages as "foreach"

constexpr byte leds[] = {8, 9, 10, 11, 12, 13};              // a byte is enough for up to pin 0 .. 255
constexpr size_t noOfLeds = sizeof(leds) / sizeof(leds[0]);  // calculate once the indexes of your array

void setup() {
  for (auto &pin : leds) {                                   // on each iteration the pin is a reference to an index of the array, the type auto determines the necessary type - in this example pin will be reference of a byte
    pinMode(pin, OUTPUT);
  }
}

void loop() {
  digitalWrite(leds[random(0, noOfLeds)], HIGH); // switch on a random LED
  delay(random(20, 200));
  digitalWrite(leds[random(0, noOfLeds)], LOW); // switch off another LED
  delay(random(20, 200));
}

constexpr is an expression which will be determined during compilation - that's ok, because these values will not change during runtime.

size_t is the type of the result of sizeof ... so you should use it (instead of int)

p.s.: please use code tags when you post code.

1 Like

jj is an odd choice of name for your loop variable, at least in context. Traditionally people use i, j or k although there is no requirement to do so - you can use whatever name you like.

Things like jj usually come in when you have deeply nested loops and have already used i, j, k and ii. Jumping straight to jj makes me wonder where the other enclosing loops are until I realize "oh, silly choice".

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.