When an object is created without a pointer, memory is reserved at compile time, but when you declare an object with a pointer, memory is reserved at run time. Which is more efficent and the more traditional practice?
When an object is created without a pointer, memory is reserved at compile time, but when you declare an object with a pointer, memory is reserved at run time. Which is more efficent and the more traditional practice?
Your understanding of memory allocation is not necessarily correct. Consider:
The only thing close to an object being allocated "at compile time" is for global variables.Code:void somefunction() { someobject obj; // <--- Allocated on stack at run time. someobject *pobj = new someobject(); // <--- Allocated from the heap at run time. ... delete pobj; // Heap allocation is persistent beyond function calls. }
Could you clarify your question?
Ok, you obviously know what I was talking about. Which wasy is more efficent and more commonly used?
> Which way is more efficent and more commonly used?
You shouldn't be looking for a simple answer that will last you forever, because there really isn't one. I believe the wisest thing to say is, unless dynamic memory provides something that you need, you should use automatic memory. And satisfy your needs. Sometimes it comes down to what other people want you to use as well. Professors will make you use dynamic memory after you've learned what pointers are, for example.
Either way, in C++ you have to learn how to work with both ways neatly because they are equally important. And you still have a ton left to learn in the STL, smart pointers, and other big libraries like Boost - this is where C++ works its strongest and you really can't make intelligent judgments on these types of questions yourself, until you've taken the time to work with them. All of the things on that list ostensibly uses stack memory by the way, but in the implementation this is not the case, making them fairly safe, flexible, and easy to use. It could be said that's what makes C++ better as a usable language.
Last edited by whiteflags; 07-07-2007 at 10:44 PM.
For a simple object that no longer needs to be used when the function is finished, you should generally use the stack. For general rules, use dynamic memory allocation in the following instances:
- You have a lot of memory to allocate. If you allocate too much locally in a function, you could blow the stack to pieces.
- You are unaware of how much memory you'll need until runtime (generally for arrays). This is more of a problem in C if you can get away with using a vector or other similar data type most of the time.
- You need what you're allocating to exist after the function ends.
Use stack arrays when you know the size at runtime, and when the size of the array is somewhat small. (I'd guess less then 10,000 bytes) There is a preset limit on a program's total stack size, but the heap is limited only by the computers total memory.
Use STL containers for everything else. In rare cases there may also be reason to write your own data structures. In even rarer cases it makes sense to write super lightweight versions of STL containers, such as by personally implementing a dynamic array.
As for variables, You should use stack variables as much as possible because they are easier to manage. But often heap variables are needed.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.