Skip to main content
gamedev.net
🔒 Locked

Stack vs heap...

Started by elis-cool Apr 12, 2002 at 8:59 PM 7 replies 200+ views
Original Post
elis-cool
elis-cool
How much stuff is too much to put on the stack before you start getting overflow errors and the such? eg should ALL variables in class''s be taken off the heap etc? CEO Plunder Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
fallenang3l
fallenang3l
I'm not sure about the stack size, but I always imagined that the OS sets aside a certain chunk of memory as stack memory (how much is up to avaible RAM and what OS you're using, I think), and if your program uses up all of that memory, you get a stack overflow. I don't know what you mean by "should all variables in classes be taken off the heap" because when you create an object of automatic storage class then all fields of that object will be on the stack too. If you create an object using dynamically allocated memory (created on heap via new operator), all data members of that object will be on the free store too. Of course, there's the third possibility that you create an object of static storage class and in that case all fields of that object will be put in the static section of memory designated by the OS. I hope I'm not wrong (just learning C++) and I hope I wasn't too confusing either. Take care .

[edited by - fallenang3l on April 12, 2002 11:28:26 PM]
lackluster
lackluster
I believe the default Stack size is 1024 bytes. You can change the size of the stack (see your compiler''s documentation).
Stoffel
Stoffel
Default stack is 1024 on some embedded systems. =)

MSVC 6.0 gives you 1 meg by default (see /F compiler option).
elis-cool
elis-cool
Well one megs more than enough isnt it...
And I mean something like so:

class AClass
{
public:
AClass()
{
Var1 = new int;
Var2 = new int;
}
// methods here
private:
int *Var1;
int *Var2;
};

AClass *instance1 = new AClass;


CEO Plunder Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
Dean Harding
Dean Harding
If you did this:


class AClass
{
public:
    AClass() {}
private:
    int var1;
    int var2;
};

AClass *inst = new AClass;


Then nothing would be created on the stack. The inst pointer would be in your data block, and the two variables AClass::var1 and AClass::var2 would be on the heap.


codeka.com - Just click it.
fallenang3l
fallenang3l
I tried searching google and VC++ 7''s docs, but I couldn''t find how to figure out the default stack size a program starts with. Is there a function call or something?
elis-cool
elis-cool
Ywah I know thats what I meant, its just the first guy seemed a little confused...

CEO Plunder Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
DrPizza
DrPizza
quote:
Original post by fallenang3l
I tried searching google and VC++ 7''s docs, but I couldn''t find how to figure out the default stack size a program starts with. Is there a function call or something?


Linker option /stack.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.