Stack Overflow Problem
I am making a (simple?) game, sort of like the "tank with angle and power" kind of game, and am trying to use classes to become more familiar with object oriented programming. Maybe this is a dumb way to do it, but what I wanted to do was have an object called map, that would contain, among other things, an array of tile objects. Each tile would have a position, whether or not it was destroyed, etc... I was going to create the game to run in 640x480 resolution, so if I was going to create one tile for each pixel, I would need 307,200 tile objects.
I have no idea if that''s a lot, but for whatever reason, my program doesn''t like it. I can create about an array of, say, 200x200 with no problem but if I try to go for the desired [480][640] array size, I get a run time error stating that my program has caused a "stack fault". Based on the fact that it will let me create fewer of the objects, I assume the problem is that I am trying to create too many, and it generates a stack fault.
So my question is two-fold:
1. How can I fix this problem? I am aware of functions such as new and malloc(), but am really not sure what they do and did not understand much better after reading the help file. Do those things apply here?
2. Is there a better way to handle terrain for a ''Scorched Earth'' type game, other than assigning an object to each pixel on the screen? I could use bigger tiles, but I''d like the terrain to be as "fine" as possible.
Let me know if seeing some of the code I have written would help; I''m not really sure would I would post if I were to do so
Thanks in advance.
Peon
I think these threads have what you''re looking for:
Worms World - A Huge Array?
2D Collision Detection, Worms
I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader''s Realm
Worms World - A Huge Array?
2D Collision Detection, Worms
I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader''s Realm
I won''t comment on your approach to storing the game world, but will focus on the error you get.
IN C/C++ data are placed three placing depending on where you write it in your program:
outside all function -> static allocation
inside a function as a local variable -> on the stack
using malloc() or new -> on the heap
Since you get a stack overflow error you have probably allocated the array within a function. The stack is generally quite small compared with the what is possible with the other two, so fix the problem you need to do one of the following:
1. Increase the stack size. In Visual C++ this is done by opening the project settings page and then going to the "Link" tab and in the list of options in the buttom add: "/STACK:1000000" (not the quotes) or some other large number of bytes that is more than you need.
OR
2. You can move the allocation outside a function and do it at global scope.
OR
3. You can use malloc() or new to allocate it dynamically. This is too involved to explain in detail here, but if you don''t know it now you might as well learn to do so, because this is a vital skill if you want to do anything remotely complex in programming. Read a C or C++ book to learn this skill. It is usually in the same section as pointers.
I hope this helps you.
Jacob Marner, M.Sc.
IN C/C++ data are placed three placing depending on where you write it in your program:
outside all function -> static allocation
inside a function as a local variable -> on the stack
using malloc() or new -> on the heap
Since you get a stack overflow error you have probably allocated the array within a function. The stack is generally quite small compared with the what is possible with the other two, so fix the problem you need to do one of the following:
1. Increase the stack size. In Visual C++ this is done by opening the project settings page and then going to the "Link" tab and in the list of options in the buttom add: "/STACK:1000000" (not the quotes) or some other large number of bytes that is more than you need.
OR
2. You can move the allocation outside a function and do it at global scope.
OR
3. You can use malloc() or new to allocate it dynamically. This is too involved to explain in detail here, but if you don''t know it now you might as well learn to do so, because this is a vital skill if you want to do anything remotely complex in programming. Read a C or C++ book to learn this skill. It is usually in the same section as pointers.
I hope this helps you.
Jacob Marner, M.Sc.
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Invader X:
I guess I shoulda did my homework and searched the forums first; didnt think to look for worms though... Thanks a bunch!
felonius:
Thanks, as well. I think I understand what you''re saying, and I''ll give it a try and see if it helps. I''ll look into malloc() like you suggested as well, because I see it all the time in other''s source, and it would suit me well to learn it.
Thanks again!
I guess I shoulda did my homework and searched the forums first; didnt think to look for worms though... Thanks a bunch!
felonius:
Thanks, as well. I think I understand what you''re saying, and I''ll give it a try and see if it helps. I''ll look into malloc() like you suggested as well, because I see it all the time in other''s source, and it would suit me well to learn it.
Thanks again!
Peon
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement