Jump to content
  • Advertisement
Sign in to follow this  
TaxCollector

problem with array memory(c++)

This topic is 3981 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
 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 code

for (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 startpoint

while(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--;}

}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
... 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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!