[GH-ISSUE #846] WiFiManagerParameter and array is not compiling/working #710

Open
opened 2026-02-28 01:26:42 +03:00 by kerem · 5 comments
Owner

Originally created by @maikheinrich on GitHub (Mar 9, 2019).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/846

Hello All,

I will use the WiFiManagerParameter in combination with an array. This seems not to work.

working example (no array):

WiFiManagerParameter custom_relais_1_off("relais_1_off", "relais_1_off", relais_1_off, 3);
WiFiManagerParameter custom_relais_2_off("relais_2_off", "relais_2_off", relais_2_off, 3);

NOT working example (array):
for (int i = 0; i < 2; i++) {
WiFiManagerParameter custom_relais_off[i]("relais_off", "relais_off", relais_off[i], 3);
}

I become an compiler error: variable-sized object 'custom_relais_auto_off' may not be initialized

How must I init this custom_relais_auto_off array ?

Issues without basic info will be ignored or closed!

Originally created by @maikheinrich on GitHub (Mar 9, 2019). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/846 Hello All, I will use the WiFiManagerParameter in combination with an array. This seems not to work. working example (no array): WiFiManagerParameter custom_relais_1_off("relais_1_off", "relais_1_off", relais_1_off, 3); WiFiManagerParameter custom_relais_2_off("relais_2_off", "relais_2_off", relais_2_off, 3); NOT working example (array): for (int i = 0; i < 2; i++) { WiFiManagerParameter custom_relais_off[i]("relais_off", "relais_off", relais_off[i], 3); } I become an compiler error: variable-sized object 'custom_relais_auto_off' may not be initialized How must I init this custom_relais_auto_off array ? Issues without basic info will be ignored or closed!
Author
Owner

@dontsovcmc commented on GitHub (Mar 27, 2019):

This is not C++.

You should use pointers:

const char *unique_id[2] = {"blah", "hmm"};
const char *relais_off[2] = {"id1", "id2"};
WiFiManagerParameter *custom_relais_off[2];
for (int i = 0; i < 2; i++) {
    custom_relais_off[i] = new WiFiManagerParameter(unique_id[i], "relais_off", relais_off[i], 3);
    wm.addParameter( custom_relais_off[i] );
}

// after using WM you should free memory:

for (int i = 0; i < 2; i++) {
    delete custom_relais_off[i];
}
<!-- gh-comment-id:476927705 --> @dontsovcmc commented on GitHub (Mar 27, 2019): This is not C++. You should use pointers: const char *unique_id[2] = {"blah", "hmm"}; const char *relais_off[2] = {"id1", "id2"}; WiFiManagerParameter *custom_relais_off[2]; for (int i = 0; i < 2; i++) { custom_relais_off[i] = new WiFiManagerParameter(unique_id[i], "relais_off", relais_off[i], 3); wm.addParameter( custom_relais_off[i] ); } // after using WM you should free memory: for (int i = 0; i < 2; i++) { delete custom_relais_off[i]; }
Author
Owner

@Gamelauncher commented on GitHub (Oct 19, 2020):

@dontsovcmc, I am struggling with this as well and although I get close, the result is not good .any suggestion? I've tried following your example with this code:

I need to be able to dynamically allocate a number of rows based on serial numbers of detectors and map them to a zone number.

    int arrArraySize = 5;
    
    WiFiManagerParameter *custom_serial[arrArraySize];

    char displaySerialVal[arrArraySize][11];
    for (int i = 0; i < arrArraySize; i++ ) {
      if (i < sizeStruct) {
        sprintf(displaySerialVal[i], "%lu", rfDetector[i].uuid);
      } else {
        sprintf(displaySerialVal[i], "%i", 0);
      }
    }

    char displaySerialId[arrArraySize][8];
    for (int i = 0; i < arrArraySize; i++ )
      sprintf(displaySerialId[i], "serial%i", i);

    char displaySerialName[arrArraySize][8];
    for (int i = 0; i < arrArraySize; i++ )
      sprintf(displaySerialName[i], "Serial %i", i);

    const char *constDisplaySerialVal[arrArraySize];
    const char *constDisplaySerialId[arrArraySize];
    const char *constDisplaySerialName[arrArraySize];

    for (int i = 0; i < arrArraySize; i++ ) {
      constDisplaySerialVal[i] = new (displaySerialVal) char[11];
      constDisplaySerialId[i] = new (displaySerialId) char[8];
      constDisplaySerialName[i] = new (displaySerialName) char[8];
    }

    for (int i = 0; i < arrArraySize; i++) {
      custom_serial[i] = new WiFiManagerParameter( constDisplaySerialId[i], constDisplaySerialName[i], constDisplaySerialVal[i], 10);
      wm.addParameter( custom_serial[i] );
    }
void * operator new[](size_t n, const char *s) {
    void *p = malloc(n);
    strcpy((char *)p, s);
    return p;
}

Results in:

            <label for='serial0'>Serial 0Serial 1Serial 2Serial 3Serial 4</label>
            <br/>
            <input id='serial0' name='serial0' maxlength='10' value='21762832'>
            <label for='serial0'>Serial 0Serial 1Serial 2Serial 3Serial 4</label>
            <br/>
            <input id='serial0' name='serial0' maxlength='10' value='21762832'>
            <label for='serial0'>Serial 0Serial 1Serial 2Serial 3Serial 4</label>
            <br/>
            <input id='serial0' name='serial0' maxlength='10' value='21762832'>
            <label for='serial0'>Serial 0Serial 1Serial 2Serial 3Serial 4</label>
            <br/>
            <input id='serial0' name='serial0' maxlength='10' value='21762832'>
            <label for='serial0'>Serial 0Serial 1Serial 2Serial 3Serial 4</label>
            <br/>
            <input id='serial0' name='serial0' maxlength='10' value='21762832'>
            <br/>
<!-- gh-comment-id:711831046 --> @Gamelauncher commented on GitHub (Oct 19, 2020): @dontsovcmc, I am struggling with this as well and although I get close, the result is not good .any suggestion? I've tried following your example with this code: I need to be able to dynamically allocate a number of rows based on serial numbers of detectors and map them to a zone number. ``` int arrArraySize = 5; WiFiManagerParameter *custom_serial[arrArraySize]; char displaySerialVal[arrArraySize][11]; for (int i = 0; i < arrArraySize; i++ ) { if (i < sizeStruct) { sprintf(displaySerialVal[i], "%lu", rfDetector[i].uuid); } else { sprintf(displaySerialVal[i], "%i", 0); } } char displaySerialId[arrArraySize][8]; for (int i = 0; i < arrArraySize; i++ ) sprintf(displaySerialId[i], "serial%i", i); char displaySerialName[arrArraySize][8]; for (int i = 0; i < arrArraySize; i++ ) sprintf(displaySerialName[i], "Serial %i", i); const char *constDisplaySerialVal[arrArraySize]; const char *constDisplaySerialId[arrArraySize]; const char *constDisplaySerialName[arrArraySize]; for (int i = 0; i < arrArraySize; i++ ) { constDisplaySerialVal[i] = new (displaySerialVal) char[11]; constDisplaySerialId[i] = new (displaySerialId) char[8]; constDisplaySerialName[i] = new (displaySerialName) char[8]; } for (int i = 0; i < arrArraySize; i++) { custom_serial[i] = new WiFiManagerParameter( constDisplaySerialId[i], constDisplaySerialName[i], constDisplaySerialVal[i], 10); wm.addParameter( custom_serial[i] ); } ``` ``` void * operator new[](size_t n, const char *s) { void *p = malloc(n); strcpy((char *)p, s); return p; } ``` Results in: ``` <label for='serial0'>Serial 0Serial 1Serial 2Serial 3Serial 4</label> <br/> <input id='serial0' name='serial0' maxlength='10' value='21762832'> <label for='serial0'>Serial 0Serial 1Serial 2Serial 3Serial 4</label> <br/> <input id='serial0' name='serial0' maxlength='10' value='21762832'> <label for='serial0'>Serial 0Serial 1Serial 2Serial 3Serial 4</label> <br/> <input id='serial0' name='serial0' maxlength='10' value='21762832'> <label for='serial0'>Serial 0Serial 1Serial 2Serial 3Serial 4</label> <br/> <input id='serial0' name='serial0' maxlength='10' value='21762832'> <label for='serial0'>Serial 0Serial 1Serial 2Serial 3Serial 4</label> <br/> <input id='serial0' name='serial0' maxlength='10' value='21762832'> <br/> ```
Author
Owner

@Gamelauncher commented on GitHub (Oct 20, 2020):

I've managed to solve this for now, wasn't paying attention to the sizes and overlooked an array indicator.

Increasing the size of the arraySize and duplicating the code to allow for zone number and zone location values as well, I hit another obstacle ..

CORRUPT HEAP: multi_heap.c:431 detected at 0x3ffedaf8
abort() was called at PC 0x4008c9e4 on core 1

This happens either directly on loading wifimanager or when requesting the param page. Ideally I would like an array size of 64, anyone have an idea on howto optimise the code a bit more for this? Heap seems just happy as after loading successfully with 32, it reports still over 90000 available. When checking the exception decoder it refers to "to many (new) operations" ?

    int arrArraySize = 5;
    
    WiFiManagerParameter *custom_serial[arrArraySize];

    char displaySerialVal[arrArraySize][11] = {};
    for (int i = 0; i < arrArraySize; i++ ) {
      if (i < sizeStruct) {
        sprintf(displaySerialVal[i], "%lu", rfDetector[i].uuid);
      } else {
        sprintf(displaySerialVal[i], "%i", 0);
      }
    }

    char displaySerialId[arrArraySize][9] = {};
    for (int i = 0; i < arrArraySize; i++ )
      sprintf(displaySerialId[i], "serial%i", i);

    char displaySerialName[arrArraySize][10] = {};
    for (int i = 0; i < arrArraySize; i++ )
      sprintf(displaySerialName[i], "Serial %i", i);

    const char *constDisplaySerialVal[arrArraySize];
    const char *constDisplaySerialId[arrArraySize];
    const char *constDisplaySerialName[arrArraySize];

    for (int i = 0; i < arrArraySize; i++ ) {
      constDisplaySerialVal[i] = new (displaySerialVa[i]l) char[11];
      constDisplaySerialId[i] = new (displaySerialId[i]) char[9];
      constDisplaySerialName[i] = new (displaySerialName[I]) char[10];
    }

    for (int i = 0; i < arrArraySize; i++) {
      custom_serial[i] = new WiFiManagerParameter( constDisplaySerialId[i], constDisplaySerialName[i], constDisplaySerialVal[i], 10);
      wm.addParameter( custom_serial[i] );
    }
void * operator new[](size_t n, const char *s) {
    void *p = malloc(n);
    strcpy((char *)p, s);
    return p;
}
<!-- gh-comment-id:713153158 --> @Gamelauncher commented on GitHub (Oct 20, 2020): I've managed to solve this for now, wasn't paying attention to the sizes and overlooked an array indicator. Increasing the size of the arraySize and duplicating the code to allow for zone number and zone location values as well, I hit another obstacle .. CORRUPT HEAP: multi_heap.c:431 detected at 0x3ffedaf8 abort() was called at PC 0x4008c9e4 on core 1 This happens either directly on loading wifimanager or when requesting the param page. Ideally I would like an array size of 64, anyone have an idea on howto optimise the code a bit more for this? Heap seems just happy as after loading successfully with 32, it reports still over 90000 available. When checking the exception decoder it refers to "to many (new) operations" ? ``` int arrArraySize = 5; WiFiManagerParameter *custom_serial[arrArraySize]; char displaySerialVal[arrArraySize][11] = {}; for (int i = 0; i < arrArraySize; i++ ) { if (i < sizeStruct) { sprintf(displaySerialVal[i], "%lu", rfDetector[i].uuid); } else { sprintf(displaySerialVal[i], "%i", 0); } } char displaySerialId[arrArraySize][9] = {}; for (int i = 0; i < arrArraySize; i++ ) sprintf(displaySerialId[i], "serial%i", i); char displaySerialName[arrArraySize][10] = {}; for (int i = 0; i < arrArraySize; i++ ) sprintf(displaySerialName[i], "Serial %i", i); const char *constDisplaySerialVal[arrArraySize]; const char *constDisplaySerialId[arrArraySize]; const char *constDisplaySerialName[arrArraySize]; for (int i = 0; i < arrArraySize; i++ ) { constDisplaySerialVal[i] = new (displaySerialVa[i]l) char[11]; constDisplaySerialId[i] = new (displaySerialId[i]) char[9]; constDisplaySerialName[i] = new (displaySerialName[I]) char[10]; } for (int i = 0; i < arrArraySize; i++) { custom_serial[i] = new WiFiManagerParameter( constDisplaySerialId[i], constDisplaySerialName[i], constDisplaySerialVal[i], 10); wm.addParameter( custom_serial[i] ); } ``` ``` void * operator new[](size_t n, const char *s) { void *p = malloc(n); strcpy((char *)p, s); return p; } ```
Author
Owner

@tablatronix commented on GitHub (Oct 20, 2020):

There is some optimization to be done on the heap that might get you more memory, all the web output uses a single $page string, and no pre allocation is done. There is an open issue discussing this #518

<!-- gh-comment-id:713198484 --> @tablatronix commented on GitHub (Oct 20, 2020): There is some optimization to be done on the heap that might get you more memory, all the web output uses a single $page string, and no pre allocation is done. There is an open issue discussing this #518
Author
Owner

@tablatronix commented on GitHub (Oct 20, 2020):

Ill try to find the relevant issue and link it

<!-- gh-comment-id:713203304 --> @tablatronix commented on GitHub (Oct 20, 2020): Ill try to find the relevant issue and link it
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#710
No description provided.