Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Making a Map from a file, stack overload error  (Read 2178 times)

0 Members and 1 Guest are viewing this topic.

Mr Random

  • Newbie
  • *
  • Posts: 4
    • View Profile
Making a Map from a file, stack overload error
« on: July 31, 2011, 06:55:54 am »
Hi guys, i'm new to both SFML and c++. Im trying to make a dungeon crawler game, im having issues when i try to read from a file containing 1's and 0's (1 for a wall, 0 for blank square).

I'm reading the file into a 2D array and using this array to tell my array of sprites to draw the walls of the level.

Im using this code to read the file (I spent a while looking around these forums trying to get this to work so it may be wrong)
Code: [Select]

map.open("layout.txt");
while(map && y<80)
{
getline(map,line);
while(x<64)
{
Board[x][y]=line[x]-'0';
x++;
}
y++;
x=0;
}

That part seems to be working fine

To draw the sprites Im using the following

Code: [Select]

for(int i=0;i<64;++i)
{
for(int j=0;j<80;++j)
{
if(Board[i][j] == 1)
{
Map[i][j].SetImage (Wall);
Map[i][j].SetPosition (WallX,WallY);
WallX = WallX + 20;
}
WallY = WallY + 10;
}
}


When i run the program I get an error saying

Quote
Unhandled exception at 0x011b33d7 in Pacman.exe: 0xC00000FD: Stack overflow.


Then visual studio shows me pointing to the probe page line
Code: [Select]

; Find next lower page and probe
cs20:
        sub     eax, _PAGESIZE_         ; decrease by PAGESIZE
        test    dword ptr [eax],eax     ; probe page.
        jmp     short cs10


I assume the error is in the way Im making my walls, is there a better way to do this?

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Making a Map from a file, stack overload error
« Reply #1 on: July 31, 2011, 07:34:00 am »
You typically get a stack overflow one of two ways:

1)  Having an array on the stack that's way too big

Code: [Select]

int func()
{
  int waytoobig[100000];  // bad idea to put all this on the stack!
}


or

2)  Having a function that's infinitely recursive (calls itself) -- or a series of functions that loop into calling themselves.

Code: [Select]

void func1()
{
  func2();
}

void func2()
{
  func1();
}

// func1 calls func2
//  and func2 calls func1
//  so this will suck up stack space until you run out


Are you doing either of these?

Mr Random

  • Newbie
  • *
  • Posts: 4
    • View Profile
Making a Map from a file, stack overload error
« Reply #2 on: July 31, 2011, 10:02:30 am »
I think I have an array that is to big. How can i fix this?
is there a command to put the array on the heap?
I know in c++ you can use new to add to an array but i haven't learn t much about using it yet, is this a good option to look into?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Making a Map from a file, stack overload error
« Reply #3 on: July 31, 2011, 10:21:49 am »
Quote from: "Mr Random"
I think I have an array that is to big. How can i fix this?
Don't use arrays, prefer containers. In this case, a dynamic container like std::vector would help.

Quote from: "Mr Random"
is there a command to put the array on the heap?
The new and delete operators. But these are really basics, you should read a good C++ book before you work with SFML, or you will have such problems again and again ;)

Quote from: "Mr Random"
I know in c++ you can use new to add to an array but i haven't learn t much about using it yet, is this a good option to look into?
Generally, dynamic memory management is important to know. But later, good code style is to avoid it where possible and use abstractions that automatically manage memory, one of which are the STL containers.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: