pointers - wrong address value being passed to function in C -
this similar incorrect pointer value passed c function there doesn't appear answer.
i have struct, called bigint
, has fields char sign
, size_t len
, uint32_t *val
. bigint
s initialized bigint_init()
function allocates memory uint32_t
array, sets default values, etc.
i have function, void bigint_subtract(bigint a, bigint b, bigint *diff)
stores a-b
in diff
. have tested function previously, follows:
bigint test = bigint_inithex("4567890987654345678987654adbfcaedcfaaebfc625213"); bigint test2 = bigint_inithex("acbefabedfc13271723976aebdfcabefc12837618237"); bigint diff = bigint_init(); bigint_subtract(test,test2,&diff); printf("0x%s - 0x%s == 0x%s\n\n",bigint_tohex(test), bigint_tohex(test2),bigint_tohex(diff));
among numerous other tests edge cases, varying lengths, etc. tests check out.
now, in function, void bigint_egcd(bigint a, bigint b, bigint c, bigint *x, bigint *y);
it's extended euclidean algorithm. in main loop of extended euclidean algorithm, have following code:
bigint rold, rnew, q, r, ...; rold = bigint_init(); bigint_cpy(&rold, a); rnew = bigint_init(); bigint_cpy(&rnew, b); ...[more initializations, etc.] while(!bigint_isval(rnew, 0)){ bigint_divide(rold,rnew,&q,&r); bigint_cpy(&tmp1, rnew); bigint_multiply(q,rnew,&tmp2); bigint_subtract(rold,tmp2,&rnew); bigint_cpy(&rold, tmp1); .... }
things going wrong, did debugging, couldn't figure out, started adding printfs. here's how looks now:
while(!bigint_isval(rnew, 0)){ bigint_divide(rold,rnew,&q,&r); printf("\t%s = %s * %s + %s\n", bigint_tohex(rold), bigint_tohex(rnew), bigint_tohex(q), bigint_tohex(r));fflush(stdout); bigint_cpy(&tmp1, rnew); printf("\tupdated r = %s - %s * %s = ", bigint_tohex(rold), bigint_tohex(q), bigint_tohex(rnew));fflush(stdout); bigint_multiply(q,rnew,&tmp2); printf("%s - %s = (%p) ", bigint_tohex(rold), bigint_tohex(tmp2), &rnew);fflush(stdout); bigint_subtract(rold,tmp2,&rnew); printf("%s @ address %p\n", bigint_tohex(rnew),&rnew);fflush(stdout); bigint_cpy(&rold, tmp1);
at beginning of bigint_subtract
, added following printf:
void bigint_subtract(bigint a, bigint b, bigint *diff){ printf("bigint_subtract diff address %p\n", diff);fflush(stdout); ....
and discovered address being received bigint_subtract
not same address passing in bigint_egcd
. meanwhile, subtraction correct, it's storing random address, , not updated value stored @ rnew
.
meanwhile, exact same code later in loop, t
, s
in place of r
counterparts, not exhibit behavior -- addresses passed correctly. rnew
, rold
initialized @ top of bigint_egcd
, in same way t
, s
counterparts are.
in short, have variable bar1
. pass &bar1
foo(type *var)
, somehow becomes else once inside of foo
. gives? kinds of mistakes cause happen?
edit
here's bigint_tohex
:
static const char *bii_hex="0123456789abcdef"; char *bigint_tohex(bigint a){ char *hex = calloc((a.len<<3) + 1,sizeof(char)); uint8_t j; for(size_t i=0; i<a.len; i++){ for(j=0; j<8; j++){ hex[ (i<<3) + j ] = bii_hex[(a.val[i] >> ((7-j)<<2)) & 0xf]; } } hex[a.len<<3] = 0; //any leading 0s should removed: int i=0; while(hex[i]=='0' && hex[i+1]!=0) ++i; memmove(hex,hex+i,strlen(hex)+1-i); if(a.sign == bii_neg){ hex = realloc(hex, (strlen(hex)+1)*sizeof(char)); memmove(hex+1,hex,strlen(hex)+1); hex[0]='-'; } return hex; }
Comments
Post a Comment