Fehler in Rekursion
-
Hallo zusammen,
ich habe mal wieder ein Problem mit einen Pointer. Ich weiß wo der Fehler auftritt, allerdings nicht warum. Vielleicht habt ihr ja einen Tip.
void add_num(uint8_t** dest /**wohin*/, void* what /** was */, size_t cnt /** wieviel */, size_t* startPos /** ab wo*/, size_t size /** quelltypgröße in Byte */) { while(cnt > 0){ //what+byte //1<<bitIndex switch (size){ case 1: if(*(uint8_t*)what & (1<<(cnt-1))){ **dest |= (1 << (*startPos)); //where }//End if break; case 2: if(*(uint16_t*)what & (1<<(cnt-1))){ **dest |= (1 << (*startPos)); //where }//End if break; case 3: if(*(uint32_t*)what & (1<<(cnt-1))){ **dest |= (1 << (*startPos)); //where }//End if break; default: if(*(uint64_t*)what & (1<<(cnt-1))){ **dest |= (1 << (*startPos)); //where }//End if break; }//End switch cnt--; (*startPos)--; if((*startPos) > 8){ (*startPos) = 7; (*dest)++; }//End if }//End while }//End add_num void encodeExpression(uint8_t* result, Expression* root, int* pos){ //encode Expression type (AND, OR, NOT, TERM) add_num(&result, &(root->type), 3, pos, sizeof(root->type)); //if not term, encode the childs if(root->type == AND_EXPRESSION || root->type == OR_EXPRESSION || root->type == UNARY_EXPRESSION){ encodeExpression(result, root->left, pos); if(root->type == AND_EXPRESSION || root->type == OR_EXPRESSION){ encodeExpression(result, root->right, pos); }//End if }//End if //leaf node else if(root->type == TERM_EXPRESSION){ //term type add_num(&result, &(root->term->type), 2, pos, sizeof(root->term->type)); //varid add_num(&result, &(root->term->varid), 3, pos, sizeof(root->term->varid)); //termop add_num(&result, &(root->term->opcode), 4, pos, sizeof(root->term->varid)); add_num(&result, &(root->term->value1), 8, pos, sizeof(root->term->varid)); }//End else if }//End encodeExpression
Der Fehler tritt auf an der Stelle wo ich encodeExpression rekursive für den rechten Teil aufrufe (root->right). Da wird irgendwie die Variable pos vermehrt und ich überschreibe die Ergebnisse von encodeExpression(root->left). Nur warum? Eigentlich bin ich doch aus dem linken Ast schon wieder zurück wenn die rechte Seite bearbeitet wird? Die Variable pos zeigt auf die aktuelle Bitposition im Ergebnisfeld (result), an die die Codierung geschrieben wird. Dies funktioniert, wenn ich nur die linke Seite bearbeite ohne Probleme.
Jemand einen Tip?
Danke.
-
wenn an einer stelle geschrieben wird, wo nicht geschrieben werden soll, ist das ein eindeutiges zeichen für pufferüberlauf.
-
Bist du sicher, daß sizeof(root->term->varid) einen Sinn ergibt?