Skip to content

Variable reset #887

Closed
Closed

Description

@GunMoe

2019-09-13_234644

Activity

  1. marmilicious commented on Sep 13, 2019

    @marmilicious
    Contributor

    On line 15, leds[num], if num is ever larger then 58 then you might be over writing bits off memory on the controller causing bad things. The value of num should only be a value of 0 to 58.

  2. henrygab commented on Oct 20, 2019

    @henrygab
    Contributor

    Marmilicious got it right.

    Details about the bug in your code.

    In C and C++, an array of length 59 is indexed from 0..58 (also known as zero-based indexing). However, your output shows that, eventually, at line 10 you executed with num set to 59. This is one index too large for the array you allocated.

    As a result, when the process uses the value 59 for the array index at line 11, it is referencing the memory immediately following the leds array.

    What's in that memory location? num is declared as a static variable immediately before leds. Compilers may (commonly?) put static variables next to each other, essentially in reverse order of their declaration. Thus, the leds array may be immediately after the memory location for num.

    How would you fix this bug?

    Delete line 18, and replace with code that does the following:

    1. If num >= NUM_LEDS, then set num to NUM_LEDS - 1, and set moe to -1.
    2. if num <= 0, then set num to zero and set moe to +1.

    Hope that helps.

  3. added
    area: core
    status: stale
    status: triage
    on Apr 16, 2026
  4. zackees commented on Aug 1, 2026

    @zackees
    Member

    Closing — this was a bug in the sketch, not in FastLED, and @marmilicious and @henrygab both diagnosed it correctly in 2019.

    Recording the answer in text (the report was a screenshot, and those don't survive well):

    The bug: leds[num] was indexed with num == NUM_LEDS. An array of 59 elements is valid for indices 0..58, so index 59 is one past the end.

    Why the symptom looked like "variable reset": @henrygab's explanation is the good part. The compiler had placed num in memory immediately before the leds array, so writing to leds[59] wrote on top of num. The variable appeared to spontaneously change value because the LED write was clobbering it.

    That's a classic and genuinely confusing failure mode — the corrupted variable and the out-of-bounds write look unrelated, and nothing in the code that touches num is wrong.

    The fix, as @henrygab gave it — clamp before writing rather than after:

    if (num >= NUM_LEDS) { num = NUM_LEDS - 1; moe = -1; }
    if (num <= 0)        { num = 0;            moe = +1; }

    Worth knowing generally: on AVR there is no memory protection, so an out-of-bounds LED write silently corrupts whatever is adjacent — another variable, the stack, anything. If a variable changes value for no visible reason, an off-by-one on an array index is the first thing to suspect. Symptoms range from a wrong number to resets to a hang, depending on what got hit.

    Closing since GitHub issues here are for FastLED bugs; for sketch help, GitHub Discussions or r/FastLED will get you a faster answer.

  5. moved this from Triage to Done in FastLED Trackeron Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corestatus: stalestatus: triage

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions