xodio / xod

XOD IDE
https://xod.io
GNU Affero General Public License v3.0
888 stars 119 forks source link

wrong read from dynamic array #2112

Open NickSZev opened 2 years ago

NickSZev commented 2 years ago

I'm trying to write node for delay of row of numbers, which enter the input sequentially. Row of nombers is random of 1-5.

in line 37 the value nomber 'i' is output from the array.

But when nValue=5, i=6, Arduino Uno outputs '60'. Wherein A[5] not equal 60.

`node { unsigned int i; int a; int *A;

int counter(unsigned int index, unsigned int nValue) {
    if (index > (nValue)) {
        return 1;
    }
    else {
        return index=index+1;
    }
}

void init() { A = new int[a]; }

void evaluate(Context ctx) {

if (isSettingUp()) { // This run once int nValue = getValue(ctx); a = nValue + 1; i = 1; init(); }

if (isInputDirty(ctx)) {

   int inValue = getValue<input_IN>(ctx);
   A[i] = inValue;
   int nValue = getValue<input_N>(ctx);
   i = counter(i, nValue);

 if(!isInputDirty<input_UPD>(ctx));

} emitValue(ctx, A[i]); emitValue(ctx, A[5]); //diagnotstic data } } data-delay.zip `

NickSZev commented 2 years ago

Found a solution: the array is increased by 1 element. I don't use the last element of the array, but now the output is correct.