The Free Store

Posts 110 of 10 · Page 1 of 1
The Free Store
What is the point of the free store? (when you use the keyword "new") I know how to use it, it's just that i don't understand why i should use it. It doesn't make sense to me. I don't really have a problem with it since i have been coding for a long time without every using it, but i have been spending this entire month trying to make my coding cleaner. So if somebody could explain to me why you would use "new" to put data on the free store instead of making a global variable, then i would really appreciate it. Thanks.
"new" is used when you want to create a new object of something, that could and should be a class for example.
Just a basic example (pseudo code):

Code:
class cTime {

public:

      int hours,minutes;

      public setTime (int Hours, int Minutes) {
            hours = Hours;
            minutes = Minutes;
}

private:

};

int main(){
      cTime *pTime = new cTime;
      pTime->setTime (12, 10);

return 0;
}

Using global variables is not always the best solution, for example if you want to destroy (destruct) an object, you can
simply use the destructor, global variables will always be stored in some register. When you initialize an object like
cObject MyNewObject; the object will be default constructed, which is now always "safe", so it's safer to "properly" construct
a new object and make use of the constructor. If something's wrong please correct me.
Thanks for the reply @FLubz. I know how to use it, it's just that it isn't making sense why i would want to. And i understand your post, it's just that it still doesn't make sense to me why i would want to do that. Just so i am able to free it from memory? That sounds more like a performance boost than anything else.



The only difference between local variables and the free store variable or variable put on the heap is that the local variables will be destroyed anyway after they are out of scope and the heap variables will be destroyed whenever you destroy it so it seems as the free store is kind of a hassle.
Think about it, imagine you have a class which holds an int.
Then an user wants to create a new instance of that class with a specific int like this new blablaClass(100);
Now imagine, what would you dow if you want, that another instance of blablaClass has another number like 200?
you simply construct new blablaClass(200);

Or maybe I simply do not understand your problem.. but ye..
There are two situations where I always use dynamic objects. Either when I'm dealing with strings or when I'm working with asynchronous operations. For example: when you are working with a socket server you create a pointer to a socket which you can pass to a handler function. The server can continue to accept new connections.
I think I understand what you're confused about. Global variables are always stored in memory while a program is executing. If you have a ton of variables, your program will be bloated and huge in memory. Storing something on the heap/free store lets you destroy that object, which means it's no longer in memory, which means your program will take up less RAM at any given time. Local variables are destroyed when they're out of scope, but in main() they are never out of scope so you have to watch that. Using new/delete ultimately reduces the size of your program in memory, which is important especially for big programs. Say you have a program that loads a document or some other file from disk and stores that in memory, or a program that creates a large chunk of data in memory. That big piece of memory would fill the shit out of your systems memory and maybe crash your program if you weren't able to "delete" the object, so using the heap/free store is good for things that take up sizeable space in memory, such as servers, photo/video editors, video games, etc. So in the end, it basically is a performance boost like you said, but a very useful and important one. With any small program, I wouldn't really worry about it though.
Thanks so much guys, this makes a lot more sense. I will always from now on keep my eye open for these kinds of situations. Thanks a bunch.
lol this thread started up weird.. but good info..
Quote Originally Posted by ImpossibleThief View Post
lol this thread started up weird.. but good info..

What do you mean it started weird?
Good to know , that your problem seems now clearer for you, a lot of guys do not give any feedback if their problem was solved.
Posts 110 of 10 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?