Jump to content
  • Advertisement
Sign in to follow this  
BennettSteele

C++ Weird memory error in while loop (SIGSEGV, Segmentation fault)

This topic is 2502 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

So im now working on a random-city generator, and it must run through the city map. for some reason though, my application crashes when it runs this segment of code, more importantly the single IF statement in the while() loop. commenting out the IF statement fixes the problem though...

char CityBuffer[50][50];
for(unsigned int Cy=0;Cy<50;Cy++){for(unsigned int Cx=0;Cx<50;Cx++)
{
CityBuffer[Cx][Cy]=' ';
}}//set to default
unsigned int Midx=rand()%50,Midy=rand()%50;
CityBuffer[Midx][Midy]='m';
for(unsigned int Rd=1;Rd<=50;Rd++)//extend road to limits
{
CityBuffer[Midx+Rd][Midy]='r';//road x+
CityBuffer[Midx-Rd][Midy]='r';//road x-
CityBuffer[Midx][Midy+Rd]='r';//road y+
CityBuffer[Midx][Midy-Rd]='r';//road y-
} for(unsigned int Cy=0;Cy<50;Cy++){for(unsigned int Cx=0;Cx<50;Cx++)
{
switch(CityBuffer[Cx][Cy])
{
case ' ':break;//nothing
case 'm':break;//middle
case 'r':
{
POINT Dir;Dir.x=0;Dir.y=0;
if(CityBuffer[Cx+1][Cy]=='r')
{
Dir.x=1;
}
else if(CityBuffer[Cx-1][Cy]=='r')
{
Dir.x=-1;
}
if(CityBuffer[Cx][Cy+1]=='r')
{
Dir.y=1;
}
else if(CityBuffer[Cx][Cy-1]=='r')
{
Dir.y=-1;
}
bool MoreRoad=true;
int RoadLength=0;
while(MoreRoad)
{
RoadLength++;
if(RoadLength==1)
{
MoreRoad=false;
}
}
//this->AddTerrain("road",Cx,1,Cy,Dir.x*RoadLength,0.5,Dir.y*RoadLength);
}break;
}
}}

Share this post


Link to post
Share on other sites
Advertisement

else if(CityBuffer[Cx-1][Cy]=='r')
{
Dir.x=-1;
}
if(CityBuffer[Cx][Cy+1]=='r')
{
Dir.y=1;
}
else if(CityBuffer[Cx][Cy-1]=='r')
{
Dir.y=-1;
}
[/quote]

If Cx or Cy are 0 or 49, they will access elements outside of array.

Share this post


Link to post
Share on other sites
POINT Dir;Dir.x=0;Dir.y=0;
if(Cx-1<49){if(CityBuffer[Cx+1][Cy]=='r')
{
Dir.x=1;
}}
else if(Cx+1>0){if(CityBuffer[Cx-1][Cy]=='r')
{
Dir.x=-1;
}}
if(Cy-1<49){if(CityBuffer[Cx][Cy+1]=='r')
{
Dir.y=1;
}}
else if(Cy+1>0){if(CityBuffer[Cx][Cy-1]=='r')
{
Dir.y=-1;
}}
bool MoreRoad=true;
if(Dir.x==0 && Dir.y==0){MoreRoad=false;}


Still breaks it...

Share this post


Link to post
Share on other sites
if(Cx+1<49){if(CityBuffer[Cx+1][Cy]=='r')
{
Dir.x=1;
}}
else if(Cx-1>0){if(CityBuffer[Cx-1][Cy]=='r')
{
Dir.x=-1;
}}
if(Cy+1<49){if(CityBuffer[Cx][Cy+1]=='r')
{
Dir.y=1;
}}
else if(Cy-1>0){if(CityBuffer[Cx][Cy-1]=='r')
{
Dir.y=-1;
}}


Still breaks... DX

Share this post


Link to post
Share on other sites
One problem is that Cx and Cy are unsigned ints, so if Cx is zero, Cx-1 is actually going to be 4,294,967,295 so it will trigger the checks for CityBuffer[Cx-1] incorrectly. I think you should simply us Cx!=0 and Cy!=0.

Share this post


Link to post
Share on other sites
DXDXDXDXDXDX

POINT Dir;Dir.x=0;Dir.y=0;
if(Cx+1<49){if(CityBuffer[Cx+1][Cy]=='r')
{
Dir.x=1;
}}
else if(Cx!=0){if(CityBuffer[Cx-1][Cy]=='r')
{
Dir.x=-1;
}}
if(Cy+1<49){if(CityBuffer[Cx][Cy+1]=='r')
{
Dir.y=1;
}}
else if(Cy!=0){if(CityBuffer[Cx][Cy-1]=='r')
{
Dir.y=-1;
}}


Still doesnt work...

Share this post


Link to post
Share on other sites

Still doesnt work...


Dude. Learn how to use a debugger.

And yes, that advice I gave you is probably the best advice anyone can give you in this thread.

Share this post


Link to post
Share on other sites
The error you are getting is caused from a buffer overrun most likely. if the code that causes the error has no errors(meaning the code is correct), then the error is elsewhere. Just because the debugger points you to that line of code as the problem does not mean what CAUSES the problem is in that line of code. What actually causes the problem sets up your if statement for failure. Go over your code and find where you are overrunning a buffer.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!