'Linker causing seemingly random crashes?

EDIT: After some more trying and testing, it seems to set down to changing stack size everytime I change code and I want the program to run. If I don't change the stack size, the program seems to crash everytime after code change.

EDIT 2: Same seems to apply to both /HEAP and /STACK

A bit of and odd question, but as far as I can tell, based on some checking and testing.

On multiple projects (at some point) I've come across this same problem:

I change a bit of code, the program crashes. I change stack size, the program doesn't crash(with the changed code), doesn't seem to matter if incrementing or decrementing stack size.

Seemingly as if there's a hidden randomized stack which, if below a certain value causing the program to crash (no error). I can cause the crash by testing different stack sizes.

On a recent project, the crash seems to "pinpoint" to the bit of code:

typedef unsigned int DINT;

template <typename LIST_ITEM>
struct LIST {
    LIST() {
        this->length = 0;
        this->total = 0;
        for (DINT i = 0; i < ENGINE_DATABASE_LIST_LENGTH_MAX; i++) {
            //this->item[i] = { 0 };
            this->existance[i] = 0;
        }
    };
    ~LIST() {

        for (DINT i = 0; i < ENGINE_DATABASE_LIST_LENGTH_MAX; i++) {
            //this->item[i] = { 0 };
            this->existance[i] = 0;
        }
        this->length = 0;
        this->total = 0;
    };

    DINT length, total;
    LIST_ITEM item[ENGINE_DATABASE_LIST_LENGTH_MAX];
    DINT existance[ENGINE_DATABASE_LIST_LENGTH_MAX];
    DINT _set(LIST_ITEM item) {
        for (DINT d = 0; d < ENGINE_DATABASE_LIST_LENGTH_MAX; d++) {
            if (this->existance[d] == 0) {
                this->item[d] = item;
                this->existance[d] = 1;
                this->length++;
                this->total++;
                return d;
            }
        }
        return 0;
    }

    void _remove(DINT position = 0) {
        this->item[position] = {};
        this->existance[position] = 0;

        
        LIST <LIST_ITEM> list = {};
        DINT length = 0;
        do {
            if (this->existance[length] == 1) {
                list._set(this->_get(length));
                this->existance[length] = 0;
                //this->item[l] = {};
            }
            length++;
        } while (length < ENGINE_DATABASE_LIST_LENGTH_MAX);
        this->_carry(list);
    }

    LIST_ITEM _get(DINT position = 0) {
        return this->item[position];
    }

    void _carry(LIST <LIST_ITEM> list = {}) {
        for (DINT d = 0; d < list.length; d++) {
            if (list.existance[d] == 1) this->_set(list.item[d]);
        }
    }

    void _deconstruct() {
        /*
        for (DINT i = 0; i < ENGINE_DATABASE_LIST_LENGTH_MAX; i++) {
            if (this->existance[i] == 1) {
                this->existance[i] = 0;
                this->item[i] = { };
            }
        }
        this->length = 0;
        this->total = 0;
        */
        this->~LIST();
    }
}; 

Possible solution/fix:

    void _remove(DINT position = 0) {
        this->existance[position] = 0;
        this->length--;
        LIST <LIST_ITEM> *list = new LIST;
        for (DINT a = 0; a < ENGINE_DATABASE_LIST_LENGTH_MAX; a++) {
            if (this->existance[a] == 1) {
                list->_set(this->item[a]);
                if (list->length == this->length) break;
            }
        }
        list->total = this->total;
        *this = *list;
        delete list;
    }

If the max list length is set to 64, the program seems to run fine, no matter of stack, but at 128 the crashes start to happen, and if I do the above (change stack size by even just 1, the program runs fine again, until next change of code, then I change stack by 1 again and the program runs fine again).

There might be a "< 0" somewhere (which could also cause crashing), which I just can't seem to spot.

Please do point out.

Seem confusing? Please ask.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source