• Advertisement

# General Memory Management Questions

This topic is 4329 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Q1) I'm trying to do something but actually don't know the word for this technique. currently my program has a main() and this main() does many things. declaring classes, classes sending messages etc. and I have come to the point where I actually have to recall main. I was not able to see this when I first started, and already the project has grown too huge. I need to loop main somehow, I've heard of calling the main function again, but for some reason, I have a general feeling that this method is evil. but instead, I want to create a new management type of class, and move everything in main() to that new management function. What do you call this? My Thoughts: This is a totally random guess, but is this what they call a control? Q2) most of my classes were declared in the stack, if I'm going to loop main() or make a new management function, I have to declare them in the heap right? My Thoughts: I should change everything to be declared in the heap. Just wanted to make sure that what I was thinking is the right direction... Q3) another question about stacks and heaps. Let's say I constructed the CLASSONE class in the heap. Then does all of CLASSONE's member variables be constructed in the heap as well, even if I didn't use "new" ? My Thoughts: I think they all stay in the heap, for example if CLASSONE constructs, a member variable CLASSTWO, and CLASSTWO constructions CLASSTHREE, all these variables will all be constructed in the heap. Thanks :D edit: did I ask too much at once? [lol] I guess I'm on the right track. Thanks Anyhow :) [Edited by - Tradone on April 19, 2006 3:16:01 PM]

#### Share this post

##### Share on other sites
Advertisement
Quote:
 Original post by TradoneQ1)I'm trying to do something but actually don't know the word for this technique.currently my program has a main()and this main() does many things.declaring classes, classes sending messages etc.and I have come to the point where I actually have to recall main. I was not able to see this when I first started, and already the project has grown too huge.I need to loop main somehow,I've heard of calling the main function again, but for some reason, I have a general feeling that this method is evil.but instead, I want to create a new management type of class, and move everything in main() to that new management function. What do you call this? My Thoughts: This is a totally random guess, but is this what they call a control?

I didn't understand the question, you ask for how this construct, your class, is called? You're basically moving everything from main to this class? Don't be afraid to reorganize things, start with a lot of small functions, preferably functions so small you don't have to scroll. Then you can compose bigger functions out of them. Other than that, I don't know what you have now or what you're going to do, but reorganizing is good. Try grouping related functions, and creating multiple files.

Quote:
 Q2)most of my classes were declared in the stack, if I'm going to loop main() or make a new management function, I have to declare them in the heap right?My Thoughts: I should change everything to be declared in the heap. Just wanted to make sure that what I was thinking is the right direction...

Maybe you are talking about a game loop? You can't loop main as far as I know. It's no problem you can declare everything on the stack and that's safer and faster too unless you have lots of code and especially lots of data (graphics, sounds).

Quote:
 Q3)another question about stacks and heaps.Let's say I constructed the CLASSONE class in the heap.Then does all of CLASSONE's member variables be constructed in the heap as well, even if I didn't use "new" ?My Thoughts: I think they all stay in the heap, for example if CLASSONE constructs, a member variable CLASSTWO, and CLASSTWO constructions CLASSTHREE, all these variables will all be constructed in the heap.

You are right

#### Share this post

##### Share on other sites
loop main:

int main(void){    while(1)    {         //this is where you put what's now in the body of your main function    }}

You'll need a way to end this loop. A primitive way is:

bool shouldStop = false;int main(void){    while( !shouldStop )    {         //this is where you put what's now in the body of your main function         if ( you want to end )         {             shouldStop = true;         }    }}

-me

#### Share this post

##### Share on other sites
As far as Heap v. Stack

Stack (generally limited to 2MB):

should be used for small data at the function scope only. Do not declare big classes on the stack.

Heap (effectively as much RAM + free HD space as your computer has):

Should be used for all of your game data and anything that survives outside of smaller utility functions. (game objects, sound data, textures, etc)

Anything instanciated on the Heap will have all of it's member variables also instantiated on the heap.

-me

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### Popular Tags

• Advertisement
• ### Popular Now

• 27
• 11
• 17
• 11
• 13
• Advertisement