Thread: Object Declaration Efficency Question

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    47

    Object Declaration Efficency Question

    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?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Your understanding of memory allocation is not necessarily correct. Consider:

    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.
    }
    The only thing close to an object being allocated "at compile time" is for global variables.

    Could you clarify your question?

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Ok, you obviously know what I was talking about. Which wasy is more efficent and more commonly used?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    > 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.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Quote Originally Posted by MacGyver View Post
    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.
    Thanks this is exactly what I was looking for. I'm just a recent CS grad trying to improve my programming skills. Currently have a good job in technology but not in programming, more hardware.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 01:08 AM
  2. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  3. synchronization object choosing
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 04:33 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM