Skip to main content
GameDev.net gamedev.net
🔒 Locked

problem with array memory(c++)

Started by TaxCollector Jul 26, 2007 at 5:39 AM 9 replies 1k views
Original Post
TaxCollector
TaxCollector
I made a function where I declared an array (of structs).Then I ran the function and the array values changed. The second time I ran that function, the array got declared again ofcourse but it still had the values from after the first run. Is there anyway to prevent this from happening? I want the array out of the memory when I'm not running the function.So that its values are reset when I run the function again.
TaxCollector
TaxCollector
 void setupAstarpath(walkerstruct &walk){	tilestruct openlist[maxpathsize];	tilestruct closedlist[maxpathsize];	tilestruct path[maxpathsize];// I'm talking about the 3 arrays above, my apologies for the sturdy codefor (int b=0;b<mapsize*4;b++){	for (int v=0;v<mapsize;v++) {		tile2[v].parent=&tile2[0][0];tile2[v].F=0;tile2[v].G=0;tile2[v].H=0;}}int i=0,j=0,index,maxF=0;openlist=tile2[spawntilex][spawntiley];i++;bool targetfound=false;while(targetfound==false && i>=0){			index=0;	maxF=openlist[0].F;	for (int k=0;k<i;k++){		if(openlist[k].F<maxF){maxF=openlist[k].F;index=k;}}	closedlist[j]=openlist[index];j++;	for(int n=index;n<i;n++){openlist[n]=openlist[n+1];}i--;	if(closedlist[j-1].inumber==walk.xtarget && closedlist[j-1].jnumber==walk.ytarget){targetfound=true;}	else{	if(((tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].taken==false || ((tile2[walk.xtarget][walk.ytarget].x==tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].x) && (tile2[walk.xtarget][walk.ytarget].y==tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].y))) && searchinlist(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2],closedlist,j)==false) && ((closedlist[j-1].inumber>=0 && closedlist[j-1].inumber<=mapsize) && (closedlist[j-1].jnumber-2>=0 && closedlist[j-1].jnumber-2<=mapsize*4))){		if(searchinlist(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2],openlist,i)){					if(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber].G+14<tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].G){				tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];				tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].G=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].parent->G+14;				tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].F=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].G+tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].H;}		}		else{		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].G=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].parent->G+14;		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].H=10*(int(fabs(float(closedlist[j-1].inumber-walk.xtarget))+fabs(float(closedlist[j-1].jnumber-2-walk.ytarget))));		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].F=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].G+tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2].H;		openlist=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-2];i++;}}	if(((tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].taken==false || ((tile2[walk.xtarget][walk.ytarget].x==tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].x) && (tile2[walk.xtarget][walk.ytarget].y==tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].y))) && searchinlist(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2],closedlist,j)==false) && ((closedlist[j-1].inumber>=0 && closedlist[j-1].inumber<=mapsize) && (closedlist[j-1].jnumber+2>=0 && closedlist[j-1].jnumber+2<=mapsize*4))){		if(searchinlist(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2],openlist,i)){					if(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber].G+14<tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].G){				tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];				tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].G=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].parent->G+14;				tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].F=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].G+tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].H;}		}		else{		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].G=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].parent->G+14;		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].H=10*(int(fabs(float(closedlist[j-1].inumber-walk.xtarget))+fabs(float(closedlist[j-1].jnumber+2-walk.ytarget))));		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].F=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].G+tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2].H;		openlist=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+2];i++;}}	if(((tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].taken==false || ((tile2[walk.xtarget][walk.ytarget].x==tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].x) && (tile2[walk.xtarget][walk.ytarget].y==tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].y))) && searchinlist(tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber],closedlist,j)==false) && ((closedlist[j-1].inumber-1>=0 && closedlist[j-1].inumber-1<=mapsize) && (closedlist[j-1].jnumber>=0 && closedlist[j-1].jnumber<=mapsize*4))){		if(searchinlist(tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber],openlist,i)){					if(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber].G+14<tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].G){				tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];				tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].G=tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].parent->G+14;				tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].F=tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].G+tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].H;}		}		else{		tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];		tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].G=tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].parent->G+14;		tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].H=10*(int(fabs(float(closedlist[j-1].inumber-1-walk.xtarget))+fabs(float(closedlist[j-1].jnumber-walk.ytarget))));		tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].F=tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].G+tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber].H;		openlist=tile2[closedlist[j-1].inumber-1][closedlist[j-1].jnumber];i++;}}	if(((tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].taken==false || ((tile2[walk.xtarget][walk.ytarget].x==tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].x) && (tile2[walk.xtarget][walk.ytarget].y==tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].y))) && searchinlist(tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber],closedlist,j)==false) && ((closedlist[j-1].inumber+1>=0 && closedlist[j-1].inumber+1<=mapsize) && (closedlist[j-1].jnumber>=0 && closedlist[j-1].jnumber<=mapsize*4))){		if(searchinlist(tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber],openlist,i)){					if(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber].G+14<tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].G){				tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];				tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].G=tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].parent->G+14;				tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].F=tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].G+tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].H;}		}		else{		tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];		tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].G=tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].parent->G+14;		tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].H=10*(int(fabs(float(closedlist[j-1].inumber+1-walk.xtarget))+fabs(float(closedlist[j-1].jnumber-walk.ytarget))));		tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].F=tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].G+tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber].H;		openlist=tile2[closedlist[j-1].inumber+1][closedlist[j-1].jnumber];i++;}}	if(((tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].taken==false || ((tile2[walk.xtarget][walk.ytarget].x==tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].x) && (tile2[walk.xtarget][walk.ytarget].y==tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].y))) && searchinlist(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1],closedlist,j)==false) && ((closedlist[j-1].inumber>=0 && closedlist[j-1].inumber<=mapsize) && (closedlist[j-1].jnumber-1>=0 && closedlist[j-1].jnumber-1<=mapsize*4))){		if(searchinlist(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1],openlist,i)){					if(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber].G+10<tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].G){				tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];				tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].G=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].parent->G+10;				tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].F=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].G+tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].H;}		}		else{		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].G=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].parent->G+10;		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].H=10*(int(fabs(float(closedlist[j-1].inumber-walk.xtarget))+fabs(float(closedlist[j-1].jnumber-1-walk.ytarget))));		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].F=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].G+tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1].H;		openlist=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber-1];i++;}}	if(((tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].taken==false || ((tile2[walk.xtarget][walk.ytarget].x==tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].x) && (tile2[walk.xtarget][walk.ytarget].y==tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].y))) && searchinlist(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1],closedlist,j)==false) && ((closedlist[j-1].inumber>=0 && closedlist[j-1].inumber<=mapsize) && (closedlist[j-1].jnumber+1>=0 && closedlist[j-1].jnumber+1<=mapsize*4))){		if(searchinlist(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1],openlist,i)){					if(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber].G+10<tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].G){				tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];				tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].G=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].parent->G+10;				tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].F=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].G+tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].H;}		}		else{		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].G=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].parent->G+10;		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].H=10*(int(fabs(float(closedlist[j-1].inumber-walk.xtarget))+fabs(float(closedlist[j-1].jnumber+1-walk.ytarget))));		tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].F=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].G+tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1].H;		openlist=tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber+1];i++;}}	if(((tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].taken==false || ((tile2[walk.xtarget][walk.ytarget].x==tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].x) && (tile2[walk.xtarget][walk.ytarget].y==tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].y))) && searchinlist(tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1],closedlist,j)==false) && ((closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)>=0 && closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)<=mapsize) && (closedlist[j-1].jnumber-1>=0 && closedlist[j-1].jnumber-1<=mapsize*4))){		if(searchinlist(tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1],openlist,i)){					if(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber].G+10<tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].G){				tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];				tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].G=tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].parent->G+10;				tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].F=tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].G+tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].H;}		}		else{		tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];		tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].G=tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].parent->G+10;		tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].H=10*(int(fabs(float(closedlist[j-1].inumber-walk.xtarget+(closedlist[j-1].jnumber % 2==0?-1:1)))+fabs(float(closedlist[j-1].jnumber-1-walk.ytarget))));		tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].F=tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].G+tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1].H;		openlist=tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber-1];i++;}}	if(((tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].taken==false || ((tile2[walk.xtarget][walk.ytarget].x==tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].x) && (tile2[walk.xtarget][walk.ytarget].y==tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].y))) && searchinlist(tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1],closedlist,j)==false) && ((closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)>=0 && closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)<=mapsize) && (closedlist[j-1].jnumber+1>=0 && closedlist[j-1].jnumber+1<=mapsize*4))){		if(searchinlist(tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1],openlist,i)){					if(tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber].G+10<tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].G){				tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];				tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].G=tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].parent->G+10;				tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].F=tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].G+tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].H;}		}		else{		tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].parent=&tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber];		tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].G=tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].parent->G+10;		tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].H=10*(int(fabs(float(closedlist[j-1].inumber-walk.xtarget+(closedlist[j-1].jnumber % 2==0?-1:1)))+fabs(float(closedlist[j-1].jnumber-1-walk.ytarget))));		tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].F=tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].G+tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1].H;		openlist=tile2[closedlist[j-1].inumber+(closedlist[j-1].jnumber % 2==0?-1:1)][closedlist[j-1].jnumber+1];i++;}}}}int l=0;int m=0;while ((closedlist[j-1].inumber != spawntilex) || (closedlist[j-1].jnumber != spawntiley)){	path[l]=closedlist[j-1];l++;	closedlist[j-1]=*closedlist[j-1].parent;}//tile2[closedlist[j-1].inumber][closedlist[j-1].jnumber] is startpointwhile(l>=0){	if(path[l].xpos-path[l-1].xpos==0 && path[l].ypos-path[l-1].ypos==-imagesize/2)	{walk.path[m]=down;m++;}	if(path[l].xpos-path[l-1].xpos==0 && path[l].ypos-path[l-1].ypos==imagesize/2)	{walk.path[m]=up;m++;}	if(path[l].xpos-path[l-1].xpos==-imagesize && path[l].ypos-path[l-1].ypos==0)	{walk.path[m]=rright;m++;}	if(path[l].xpos-path[l-1].xpos==imagesize && path[l].ypos-path[l-1].ypos==0)	{walk.path[m]=lleft;m++;}	if(path[l].xpos-path[l-1].xpos==imagesize/2 && path[l].ypos-path[l-1].ypos==imagesize/4)	{walk.path[m]=upleft;m++;}	if(path[l].xpos-path[l-1].xpos==imagesize/2 && path[l].ypos-path[l-1].ypos==-imagesize/4)	{walk.path[m]=downleft;m++;}	if(path[l].xpos-path[l-1].xpos==-imagesize/2 && path[l].ypos-path[l-1].ypos==imagesize/4)	{walk.path[m]=upright;m++;}	if(path[l].xpos-path[l-1].xpos==-imagesize/2 && path[l].ypos-path[l-1].ypos==-imagesize/4)	{walk.path[m]=downright;m++;}	if(l==0)	{walk.path[m]=finish;}	l--;}}
Gage64
Gage64
Quote:
The second time I ran that function, the array got declared again ofcourse but it still had the values from after the first run.


How do you know it had the same values? You're resetting them each time the function is called.

Also, please use "source" and "/source" tags, both inside [] and without the "" to post long code snippets.
TaxCollector
TaxCollector
I checked their values after they were declared in the second run, using the debugger in visual studio 2005
klajerhgkja
klajerhgkja
Quote:
Original post by TaxCollector
I checked their values after they were declared in the second run, using the debugger in visual studio 2005
The values aren't resetted because that would be wasteful. When you return from the function, the values in the stack stay as they were. When you call the same or some other function, that function will get the memory space that used to contain the previous function's variables in it. You're supposed to initialize the local variables (and arrays) so that they contain what you want (and not what was there previously). You should not rely on any kind of compiler switch for this even if such exists. You should just reset the values yourself like you do now.

If the compiler did this to you, it would have to do it the same way that you do it, with the same instructions so it wouldn't be any faster. But it would always reset the variables to some constant value e.g. 0 and when you wanted them to be, say, 1, you'd have to immediately overwrite the 0 with 1. So that would add wasted work.

As for memory usage: The stack has a constant size, e.g. one megabyte, so that memory can't be freed. You should not worry about this. The memory is "freed" automatically every time you return from a function, so the next function can use the same memory location again. If you try to allocate too large arrays in stack, your program will crash because of the one meg limit. Such arrays should be allocated from the heap (with operator new or e.g. std::vector), and when done so, that memory can (and should) actually be released once you're done with it. But when you allocate more memory after releasing some, you may get the same memory block containing the old values in it again. So once again, initialize the memory range yourself.
TaxCollector
TaxCollector
ok thanks for the explanation :)
I guess I'll reset them myself
phil_t
phil_t
You don't need to bother "resetting" them unless you read the values before you write them in that function. And if you're doing that, you have a bug in your code, since they weren't guaranteed to be zero the first time around either - you're basically reading garbage.
rip-off
rip-off
Give tilestruct a sane default constructor.
Zahlman
Zahlman
... The hell is that? x.x

Here's a first cut at cleaning up all the repeated stuff. Please check that I didn't mess anything up :)

void setupAstarpath(walkerstruct &walk) {	tilestruct openlist[maxpathsize];	tilestruct closedlist[maxpathsize];	tilestruct path[maxpathsize];	const int width = mapsize;	const int height = mapsize * 4;		for (int b = 0; b < height; b++) {		for (int v = 0; v < width; v++) {			tile2[v].parent = &tile2[0][0];			tile2[v].F = 0;			tile2[v].G = 0;			tile2[v].H = 0;		}	}		int i = 0, j = 0, index, maxF = 0;	openlist = tile2[spawntilex][spawntiley];	i++;	while (i >= 0) {			index = 0;		maxF = openlist[0].F;		for (int k = 0; k < i; k++) {				if (openlist[k].F < maxF) {				maxF = openlist[k].F;				index = k;			}		}		closedlist[j++] = openlist[index];		for (int n = index; n < i; n++) {			openlist[n] = openlist[n + 1];		}		i--;				int current_x = closedlist[j - 1].inumber;		int current_y = closedlist[j - 1].jnumber;		if (current_x == walk.xtarget && current_y == walk.ytarget) {			break; // target found		}		int x_adjust = current_y % 2 == 0 ? -1 : 1;		tilestruct* neighbours = {			tile2[current_x][current_y - 2],			tile2[current_x][current_y + 2],			tile2[current_x - 1][current_y],			tile2[current_x + 1][current_y],			tile2[current_x][current_y - 1],			tile2[current_x][current_y + 1],			tile2[current_x + x_adjust][current_y - 1],			tile2[current_x + x_adjust][current_y + 1],		};		tilestruct& current = tile2[current_x][current_y];		tilestruct& target = tile2[walk.xtarget][walk.ytarget];		for (int n = 0; n < sizeof(neighbours) / sizeof(neighbours[0]); ++n) {			tilestruct& adjacent = *neighbours[n];			if ((!adjacent.taken || (target.x == adjacent.x && target.y == adjacent.y)) 						&& !searchinlist(adjacent, closedlist, j)						&& adjacent_x >= 0 && adjacent_x <= width						&& adjacent_y >= 0 && adjacent_y <= height) {				if (searchinlist(adjacent, openlist, i)) {					if (current.G+14 < adjacent.G) {						adjacent.parent = &current						adjacent.G = current.G + 14;						adjacent.F = adjacent.G + adjacent.H;					}				} else {					adjacent.parent = &current					adjacent.G = current.G + 14;					adjacent.H = 10 * (int(fabs(float(adjacent_x - walk.xtarget)) +																 fabs(float(adjacent_y - walk.ytarget))));					adjacent.F = adjacent.G + adjacent.H;					openlist[i++] = adjacent;				}			}		}	}		int l = 0;	for (tilestruct* t = &closedlist[j - 1];	     (t->inumber != spawntilex) || (t->jnumber != spawntiley);	     t = t->parent) {		path[l++] = *t;	}	const int halfsize = imagesize / 2;	const int quartersize = imagesize / 4;	int m = 0;	for (; l > 0; --l) {		int dx = path[l].xpos - path[l - 1].xpos;		int dy = path[l].ypos - path[l - 1].ypos;				if (dx == 0 && dy == -halfsize) {			walk.path[m++] = down;		}		if (dx == 0 && dy == halfsize) {			walk.path[m++] = up;		}		if (dx == -imagesize && dy == 0) {			walk.path[m++] = rright;		}		if (dx == imagesize && dy == 0) {			walk.path[m++] = lleft;		}		if (dx == halfsize && dy == quartersize) {			walk.path[m++] = upleft;		}		if (dx == halfsize && dy == -quartersize) {			walk.path[m++] = downleft;		}		if (dx == -halfsize && dy == quartersize) {			walk.path[m++] = upright;		}		if (dx == -halfsize && dy == -quartersize) {			walk.path[m++] = downright;		}	}	// now l == 0	walk.path[m] = finish;}
mattnewport
mattnewport
If tilestruct is just a POD struct with no constructor and you just want to clear the array memory to 0 you can just do:
tilestruct openlist[maxpathsize] = {};

As others have pointed out it shouldn't be necessary if your code is bug free and there is a small performance cost associated with clearing the array but it's not bad practice and it can help to catch bugs due to uninitialized array entries. Depending on the code it might be more appropriate to give tilestruct a default constructor that sets its members to sensible default values.

If you're using VS 2005 another thing you might want to do if you don't want to initialize the array is to set the 'Basic Runtime Checks' option under Project Properties -> Configuration Properties -> C/C++ -> Code Generation to 'Both' which will clear all stack memory to 0xcc when entering a function and will check for overruns of arrays on the stack and catch some uses of uninitialized variables.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.