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

Started by
14 comments, last by BennettSteele 12 years, 2 months ago
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;
}
}}
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.
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...
Why do you think Cx - 1 < 49 would work to see if Cx + 1 is out of bounds?
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
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.
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...

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.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
DX Anyways, its not the Cx or Cy; its the IF statement inside the while loop.
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.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

This topic is closed to new replies.

Advertisement