Saturday, 28 September 2013

segmentation fault when I return

segmentation fault when I return

this code segfaults when it returns. I have tried to put the code inline
in main however it fails when it leaves the while loop. Any help you have
is appreciated!
code is called from here
if (inputType == 'M')
{
ReadMap(holder, LvlSize, Levels, start);
}
else if (inputType == 'L')
{
LmodeInput(holder, LvlSize, Levels, start);
}
//code should read in coordinate and initialize a map based on
coordinates
void LmodeInput(vector<vector <square> > &holder,int size, int
levels,square&start)
{
int row = 10;
int column = 10;
int level = 10;
char x = '1';
string trash = "10";
initializeVector(holder, size, levels); //initialize all to .
while (1)
{
if (!(cin >> x))
{
cout << "this should execute";
printMap(holder, size, levels);
cout <<endl;
cout << "break should execute" <<endl;
break;
}
if (x == '/')
{
getline(cin, trash);
continue;
}
cin >> row;
cin>>x;
cin>>column;
cin >>x;
cin>>level;
if (row > (size - 1) || column > (size - 1))
{
cout << "out of bounds" <<endl;
exit(1);
}
else if ( row < 0 || column < 0)
{
cout << "out of bounds" <<endl;
exit(1);
}
else if (level > 10 || level < 0)
{
cout << "out of bounds" <<endl;
exit(1);
}
cin >> x; //,
cin >> x;
if (x == '#' || x== 'E' || x == 'S' || x== 'H' ||x == '.')
{
holder[size*((levels-1)-level) + row][column].type = x;
if (x == 'S')
{
start = holder[size*((levels-1)-level) + row][column];
}
}
else
{
cout << "Invalid character" <<endl;
exit(1);
}
cin >> x;
}
cout << "return will execute";
return
}
this function segfaults when it hits the return statement. all gdb tells
me is that the segfault is in the return
valgrind provides this output:
Invalid write of size 1
==26557== at 0x401776: main (project1.cpp:188)
==26557== Address 0x20 is not stack'd, malloc'd or (recently) free'd
==26557==
==26557==
==26557== Process terminating with default action of signal 11 (SIGSEGV)
==26557== Access not within mapped region at address 0x20
==26557== at 0x401776: main (project1.cpp:188)
==26557== If you believe this happened as a result of a stack
==26557== overflow in your program's main thread (unlikely but
==26557== possible), you can try to increase the size of the
==26557== main thread stack using the --main-stacksize= flag.
==26557== The main thread stack size used in this run was 10485760.

No comments:

Post a Comment