Stack overflow.

Started by
32 comments, last by Calin 14 years, 8 months ago
Quote:data on the stack in main() - in which case you need to switch to allocating some data elsewhere (I.e. on the heap)


what do you mean, can someone detail this a bit.

My project`s facebook page is “DreamLand Page”

Advertisement
Quote:Original post by Calin
Quote:data on the stack in main() - in which case you need to switch to allocating some data elsewhere (I.e. on the heap)


what do you mean, can someone detail this a bit.
Every local variable takes up some stack space (And every member variable in every class you have instanced as a local variable). On Windows by default you have 1MB of stack. If all of your local variables total more than 1MB, you'll get a stack overflow.

Try commenting out the code from the "Map and characters" comment to the end of main(). If the problem goes away, you know it's in there somewhere.

I'd hazard a guess that your Map class (or one of your other ones) has a crapload of member variables.
I got the code to run by switching out several lines. The light class is making the trouble. [edit] found this thread googling chkstk.asm
http://www.gamedev.net/community/forums/topic.asp?topic_id=296695

Quote:Original post by LessBreadchkstk (check stack) is a compiler intrinsic function that "touches" stack pages to ensure they are paged in before they are used. On Windows, the default size of the stack is 1 mb, but all of that memory is not paged in at the outset. The compiler inlines the chkstk function when it encounters stack variables (ie local function variables) larger than one page. eg array[128][128]. If that was an array of char, it would require 16kb or 4 pages (1 page == 4096 bytes). With a name like vertices, I suspect the size of each element of the array is a lot more than one byte. At any rate, chkstk, advances the stack pointer page by page, "touching" each page as it proceed through a loop counting down the number of pages the variable requires. It makes sense that the stack overflow exception lands in chkstk, because the "touching" instruction produces the fault when the stack pointer has gone beyond the stack limit.


I have an array of 256x256
color LightMap[256][256]; (each color has 4 ints) Is this is too big how do I solve it?

[Edited by - Calin on July 31, 2009 1:17:43 PM]

My project`s facebook page is “DreamLand Page”

Quote:I have an array of 256x256
color LightMap[256][256]; (each color has 4 ints) Is this is too big how do I solve it?


I don't think the array is that big, it should be able to fit on the stack. But you could try allocating it on the heap to be sure, just make sure you 'delete[]' it after using it.
Quote:
I don't think the array is that big, it should be able to fit on the stack. But you could try allocating it on the heap to be sure, just make sure you 'delete[]' it after using it.


How do you allocate on heap?

int *blah = new int[4][4];
delete [] blah;

My project`s facebook page is “DreamLand Page”

Quote:
How do you allocate on heap?

int *blah = new int[4][4];
delete [] blah;


Yep, that's pretty much it. I doubt allocating on the heap will solve the problem, but it's worth a try. On a sidenote, avoid allocating variables on the heap unless it's absolutely neccessery. Unlike variables allocated on the stack, which are deleted for you, you have to manually delete the allocated memory when it's done and this is prone to errors.

EDIT: I found a thread which may answer some questions you have about what the heap actually is and the implications of using it:
http://www.gamedev.net/community/forums/topic.asp?topic_id=313157
Quote:Original post by Calin
Quote:
I don't think the array is that big, it should be able to fit on the stack. But you could try allocating it on the heap to be sure, just make sure you 'delete[]' it after using it.


How do you allocate on heap?

int *blah = new int[4][4];
delete [] blah;


Not quite. A multidimensional array on the heap is actually an array of arrays, and thus the array must contain pointers to point to those arrays.

int** array = new int*[4];
for(int i = 0; i < 4; i++)
{
array = new int[4];
}

Something along those lines should do it for you.
It feels almost like a sin not being able to declare large array without pointers.
anyways one more question

class x
{
int y;
}

x* _MyX = new x[100]; Why do I access member variables through pointer as if _MyX is a regular varibale? _MyX[10].y = 10; (instead of using the -> sign)

My project`s facebook page is “DreamLand Page”

Quote:Original post by Chadwell
Not quite. A multidimensional array on the heap is actually an array of arrays, and thus the array must contain pointers to point to those arrays.

You are confusing array of arrays with arrays of pointers. True multidimensional arrays on the heap are possible, it's just that the syntax looks a little funny.
int (*a)[4] = new int[4][4];delete[] a;

If the C declarator syntax gives you nightmares, use a typedef.
typedef int row[4];row *a = new row[4];delete[] a;
Quote:Original post by Calin
It feels almost like a sin not being able to declare large array without pointers.

Using arrays is almost always a sin in itself. There's a better solution:
std::vector<int> large(10000000); // 40 MB of fresh memory initialized to 0

Quote:Original post by Calin
Why do I access member variables through pointer as if _MyX is a regular varibale? _MyX[10].y = 10; (instead of using the -> sign)

Because the type of _MyX[10] is x, not pointer to x. The pointer is "consumed" when you use the index operator [].

This topic is closed to new replies.

Advertisement