Thread: When to Allocate or Not to Allocate

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    When to Allocate or Not to Allocate

    What factors should one use to determine when you should use the new and delete operators? I am completely comfortable with pointers, is there a reason to dynamicaly allocate members of a class, if the class instance its self is dynamically allocated? Should all the members of a class be pointers, or just some of them? How do you know whats the best? Whats the speed impact? Whats the best way to do it? I clearly have alot of questions. Does someone have a good link to an article on the subject?

    I am taking a hard long look at where I am in the development in my program before I continue any father. Im looking at basic concepts of my program and trying to decide if I am doing things the best way they can be done. This is one of the gray areas I have been debating with myself.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    is there a reason to dynamicaly allocate members of a class, if the class instance its self is dynamically allocated?
    It depends entirely on what you are intending to dynamically allocate. Arrays and other dynamic content are the only members that should obviously be dynamically allocated.

    Should all the members of a class be pointers?
    No, unless all members satisfy the above condition.

    How do you know whats the best?
    Dynamically allocated members are not initialized until you, the programmer, tell the computer to do so. So in the event that maybe you don't want to allocate some memory unless you need to you should make it dynamic.

    Whats the best way to do it?
    There is no one "best" way, you just have to use an ounce of skill with a cup of common sense.

    Does someone have a good link to an article on the subject?
    Have you tried reading the FAQ?

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Use vectors, forget that the new and delete operators exist.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    I read the FAQ and all the articles before I posted, I dont think im getting my point across, or either you think I just started with C++. Lets take this for example.
    Code:
    class A {
    public:
    A() {
      i = new int[30];
    }
    ~A() {
      delete i;
    }
    private
      int* i;
    };
    
    class B {
    public:
    
    private:
      int i[30];
    };
    Both will work and offer the same function, if things work the way I think, then B has a larger footprint in active memory than A. And in the case of very large classes that may take up KBs of space, how does this play out? What are the pros and cons of the two methods?

    Dynamically allocated members are not initialized until you, the programmer, tell the computer to do so. So in the event that maybe you don't want to allocate some memory unless you need to you should make it dynamic.
    I think 'initialized' may not have been the proper word, or I may just be confused. So, your saying that if the class needs the data member to always exist during the course of the program execution, then the data member in question should not be dynamically allocated. Even if the data member is very large? Having very large data members declared directly into the class will bloat the memory size of the class, correct?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Between class A and B, use B, because A is wrong (it should be delete [] i).

    Even if you fix A, you should generally use B. Prefer statically allocated memory over dynamic in general. Dynamically allocating memory requires that you free it, which adds complexity to your code (see the incorrect deletion in your example).

    One case in which you might want to use dynamic memory include times when that array is very large. Since the stack has a much smaller limit than the heap, you don't want to use up the stack memory with your array. Use vectors to automatically handle that dynamic memory. Another case in which you might need to allocate dynamically is if you might not need to use the array in the class (your other question). If you have a larger array, but will only rarely use it, you can leave the pointer set to null and only allocate the memory when necessary.

    Of course, you should be using vectors for dynamic arrays anyway, and your vectors do not themselves need to be dynamically allocated. So really, only in a few specific circumstances should use dynamially allocate an array.

    There are also often better design solutions to avoid dynamically allocating member objects (not arrays). In general you should use automatic allocation because it is clearer, safer, and faster.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Try this: don't dynamically allocate any memory for any program. If you can do it, then no worries. If you can't, then use dynamically allocated memory.
    Code:
    class A {
    public:
    A() {
      i = new int[30];
    }
    ~A() {
      delete i;
    }
    private
      int* i;
    };
    
    class B {
    public:
    
    private:
      int i[30];
    };
    What if you may need to store 10,000 ints or 5 ints, but you aren't sure which until you get some user input? You could write the program with an int array: i[10000]. But, if you only end up needing room for 5 ints, then you set aside memory that you didn't need. That's not real efficient, but with ints that's probably no big deal. Now what if you didn't know whether you were going to need to store 10,000 objects or 5 objects, where each object had 30 member variables, some of which were other objects. The memory required may be significant, so you might want to wait until runtime to allocate the memory to see how much memory you really need.

    The approach mentioned by anonytmouse is to forget about dynamically allocating memory and use vectors and the other containers provided in the C++ Standard Template Library. Vectors start off empty and they can grow their size automatically. So, if you need to store 10,000 things, you add 10,000 things to the vector; if you only need to store 5 things, then you just add 5 things to the vector. A beginning C++ book called "Accelerated C++"(Koenig, Moo) takes that approach and starts off teaching the reader how to use vectors--before even explaining what arrays and pointers are. Personally, I think the book is a bit advanced for a beginner. It's fairly short as well--maybe an inch thick--so you might want to check it out.
    Last edited by 7stud; 11-14-2005 at 01:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 04:19 PM
  2. Replies: 5
    Last Post: 03-23-2009, 03:44 PM
  3. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  4. Why can't you allocate a 2D array like this?
    By rudyman in forum C++ Programming
    Replies: 7
    Last Post: 07-22-2008, 01:47 PM
  5. when to allocate memory
    By SAMSAM in forum Windows Programming
    Replies: 3
    Last Post: 01-22-2003, 11:40 PM