String not printing depending on table size(?)

I get following output where a String, "/WorkShopgreg" is printed or not printed from setup depending on whether the last entry of tables are #if'd out. these are separate executions of a program (not my code)

Waiting for MQTT message!
/WorkShopgreg
Waiting for MQTT message!

Waiting for MQTT message!
/WorkShopgreg
Waiting for MQTT message!

Code:

# include <Arduino.h>

//Sets LED data table
int ledCount = 7;
struct led {
    String LedName;
    int    LedPinNumber;
    boolean LedPinState;
    String LedChannel;
    boolean TrapEmailStatus;
    String StatusMessage;
    String NodeName;
    String StatusMessageArmed;
}

LED[] = {
    { "trap1A",              19, 0, "/WorkShop/Trap1Status",      0, "WS Trap 1 Fired!", "WorkShop",     "WS Trap 1 is armed.",   },
    { "trap2A",               5, 0, "/WorkShop/Trap2Status",      0, "WS Trap 2 Fired!", "WorkShop",     "WS Trap 2 is armed.",   },
    { "Trap3A",              18, 0, "/WorkShop/Trap3Status",      0, "WS Trap 3 Fired!", "WorkShop",     "WS Trap 3 is armed.",   },
    { "trap1B",              13, 0, "/SummerHouse/Trap1Status",   0, "SH Trap 1 Fired!", "SummerHouse",  "SH Trap 1 is armed.",   },
    { "trap2B",              12, 0, "/SummerHouse/Trap2Status",   0, "SH Trap 2 Fired!", "SummerHouse",  "SH Trap 2 is armed.",   },
    { "trap3B",              14, 0, "/SummerHouse/Trap3Status",   0, "SH Trap 3 Fired!", "SummerHouse",  "SH Trap 3 is armed.",   },
#if 1
    { "trap4B",              16, 0, "/SummerHouse/Trap4Status",   0, "SH Trap 4 Fired!", "SummerHouse",  "SH Trap 4 is armed.",   },
#endif
};

//Data table MQTT subscriptions
int SubTopicsMax = 7;
struct SubTopics {
    String SubTopicName;
}

SUBTOPIC[] = {
    {"/WorkShop/Trap1",     },
    {"/WorkShop/Trap2",     },
    {"/WorkShop/Trap3",     },
    {"/SummerHouse/Trap1",  },
    {"/SummerHouse/Trap2",  },
    {"/SummerHouse/Trap3",  },
#if 0
    {"/SummerHouse/Trap4",  },
#endif
};

// -----------------------------------------------------------------------------
String myPayload = "/WorkShopgreg";

void setup ()
{
    Serial.begin (9600);
        Serial.println (myPayload);
    Serial.println ("Waiting for MQTT message!");
}

// -----------------------------------------------------------------------------
void loop ()
{
}

Looks correct, what were you expecting?

with code #if'd in

Low memory ?

Does the last structure element need a , after the }?

Sketch uses 4172 bytes (12%) of program storage space. Maximum is 32256 bytes.
Global variables use 1118 bytes (54%) of dynamic memory, leaving 930 bytes for local variables. Maximum is 2048 bytes.

My guess is that you are running out of heap and the allocation of space for String myPayload = "/WorkShopgreg"; is failing. Print out myPayload.length() to see if it is zero length.

When I changed all your "String" objects to "const char *" the "Global variable" (RAM) use went from 1142 bytes to 244 bytes.

The last elements in your structures have a trailing ,

Totally allowed. Historic.

a7

Really? looks wrong, like the compiler would think more to come. Is it optional or mandatory? This is one of the reasons I read here, It helps me learn Cxx obscura. Thanks for the details!

What is odd is that the amazing awesome optimising compiler shouldn't care about anything other than

String myPayload = "/WorkShopgreg";

void setup ()
{
    Serial.begin (9600);
        Serial.println (myPayload);
    Serial.println ("Waiting for MQTT message!");
}

// -----------------------------------------------------------------------------
void loop ()
{
}

@gcjr your two separate executions look identical. The code should only print two lines. So what code were you running not the code you posted?

I get

/WorkShopgreg
Waiting for MQTT message!

No matter the if 1 / if 0... wokwi / UNO.

What board and IDE are you using?

a7

The seemingly superfluous or erroneous comma at the end of the list is optional.

I don't know for sure, but it may have saved someone some trouble with automated production of tables of initialisers back in the day, someone with the power to say "screw it, Imma let that comma be there so I don't have to make sure it isn't" who managed to make it a fact.

a7

Cool, cool.

Well you just gave me an excuse to google it, THX. For some reason, I always write that comma there, just because it is allowed. :expressionless:

If you have no life the time to spare, google leads to some good stuff beyond my guessing.

a7

Uno

Have chased that extra delimiter issue too many times. It looks wrong, but it does eliminate that error entirely!

BTW: what did you search giggle for? "trailing comma"?

length is 0 when it fails to print

does this mean that "String"s space is unknown at compile time because each is initialized at run-time from heap?

not sure what this means. suggests String consumes a log to RAM

Something like

 C++ comma at end of initialiser list

Yeah, that worked just now, so.

a7

@gcjr your minimum example does not produce the output you claim, and does not make an error as you are asking about.

Not at this latitude and longitude, anyway.

Please post a minimum example,e that does illustrate your problem.

a7

Yes. The memory that the String objects use to store the characters is allocated dynamically when the objects are initialized. The string constants start in FLASH and are copied to RAM before the sketch starts. They are then copied to more RAM when they are copied into the String objects.

curious, what are you testing on?