file - Debugging a C program using Structs -
i attempting write program sorts speed dating information credit in computer science 1 course; , running odd problem. program when get's series of nested loops crashes after running loop twice (at point debug function called). function transferdata; designed take data file , move structures later processing yet written function. life of me can't figure out advice welcome, below i've placed code , below test data using.
cadata transferdata(fdata * filedata, cadata * workingdata, int numcouples) { int = 0, j = 0, k = 0; workingdata->madata = (struct mdata*)malloc(sizeof(mdata)*(numcouples*2));//create array of match data cells equal double number of couples workingdata->madata->coudata = (struct cdata*)malloc(sizeof(cdata)*numcouples);//create couple structure array number of cells equal number of couples. ///todo: //read in names for(i = 0; < (numcouples * 2); i++)//rotate through matches { if(i < numcouples)//men first { debug(); for(j = 0; j < numcouples; j++)//scan in first half of couples { for(k = 0; k < numcouples; k++)//scan in male names { fscanf(filedata->inputdata, "%s", workingdata->madata[i].coudata[j].guyname); } for(k = 0; k < numcouples; k++)//scan in female names { fscanf(filedata->inputdata, "%s", workingdata->madata[i].coudata[j].girlname); } } } else//now women { for(j = 0; j < numcouples; j++)//scan in first half of couples { for(k = 0; k < numcouples; k++)//scan in female names { fscanf(filedata->inputdata, "%s", workingdata->madata[i].coudata[j].girlname); } for(k = 0; k < numcouples; k++)//scan in male names { fscanf(filedata->inputdata, "%s", workingdata->madata[i].coudata[j].guyname); } } } } //read in scores //compute differances }
edit: condensed code location of error, it's debug function called; runs twice, crashes. it's returning -1073741819 (0xc0000005).
fscanf()
takes file*
argument, have passed fdata*
. unless alias file*
won't sensible, , if type alias (typedef), why obfuscate in manner?
you assigning elements of structure passed teh function. without showing passing , definition of structure not possible verify correctness of code shown, common error when using fscanf()
assign string overrun buffer or not have allocated buffer in first place. in case have ask whether guyname
, girlname
members arrays of sufficient length. pointers, need allocate space data. might modify format specifier ensure long string cannot copies these members.
if caller passing in workingdata
should caller's responsibility allocate necessary data. bad practice allocate dynamic memory locally , pass caller - begs question of , when memory free'd , asking memory leek.
in end simplest, fastest , best way debug code step through in symbolic debugger.
Comments
Post a Comment