C++ Dynamic allocation within a loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JonathanS
    New Member
    • Jan 2008
    • 37

    C++ Dynamic allocation within a loop

    I have a program in which I am trying to create a chain of (unspecified number) nodes. I want to be able to use a loop to create a new node and then link it in. I've tried it (and I think it works) to put the new within the loop and just have the compiler allocate a new version of the variable using the same name.

    E.g.,
    Code:
     for (      )
                         Node * anode = new Node();
    This doesn't seem to be the best way to do things.... I also tried creating an array of Node pointers beforehand and then doing the linking in a subsequent loop. Is there a more effective approach to this type of situation.

    Thanks for any help. Sorry for not offering a more concrete example in the code.
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    If you need ability to delete these nodes one-by-one there will always be N calls to memory allocation routine and N call to Node() constructor, I don't see how it can be faster if you put it in separate loop.

    Comment

    • JonathanS
      New Member
      • Jan 2008
      • 37

      #3
      To clarify...

      I meant that the array method would give each instance of the variable a unique identifier within the program whereas if I run through the for loop 5 times I have 5 objects I can't access by name. I understand your point about speed.

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        I cant clearly get ur requirement.
        You want to store all the elements in a arry or want to store it as a linked list.?

        Raghu

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          Originally posted by JonathanS
          I have a program in which I am trying to create a chain of (unspecified number) nodes. I want to be able to use a loop to create a new node and then link it in. I've tried it (and I think it works) to put the new within the loop and just have the compiler allocate a new version of the variable using the same name.

          E.g.,
          Code:
           for (      )
                               Node * anode = new Node();
          This doesn't seem to be the best way to do things.... I also tried creating an array of Node pointers beforehand and then doing the linking in a subsequent loop. Is there a more effective approach to this type of situation.

          Thanks for any help. Sorry for not offering a more concrete example in the code.
          Depends on what do you want.Are those nodes going to be searched frequently?(Arr ay is faster)
          Will you have a need to add a node to the top or the bottom?(List can be made faster)
          Do you want it to dynamically resize easily?(List is faster)

          Answer those questions and you should know whether do you need to use lists or array of pointers to Node.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            You should not be using an array at all. You should be using an STL <vector> by default. If you have a reason like needing to be able to search or needing to be able to insert/delete from anywhere other than the end then consider one of the other STL containers.

            Comment

            • JonathanS
              New Member
              • Jan 2008
              • 37

              #7
              Follow up

              Thanks for all of the advice on the matter. The vector container seems like a better bet. I'm still troubled by whether or not (outside of this particular case) creating instantiations of an object with new within a loop structure is good practice.

              To gpraghuram: I wanted to ultimately use a linked list, but didn't know how to get a set of pointers in a loop due to the above issue

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                If you know the number of iterations of the loop before you start it then you could have a vector of objects (rather than pointers) and just set its size before the loop starts. That would allocate your data in 1 large block.

                Comment

                • jtgd
                  New Member
                  • Dec 2008
                  • 2

                  #9
                  Originally posted by JonathanS
                  Thanks for all of the advice on the matter. The vector container seems like a better bet. I'm still troubled by whether or not (outside of this particular case) creating instantiations of an object with new within a loop structure is good practice.
                  In any case you must keep track of the memory that you new so you can delete it later, whether it is in an array of pointers or a linked list.

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Originally posted by JonathanS
                    I'm still troubled by whether or not (outside of this particular case) creating instantiations of an object with new within a loop structure is good practice.
                    It isn't bad practice in general if that what the program logic calls for.

                    What you have to watch out for is your over-all memory allocation scheme, for instance a logic that requires that you are constantly allocating and deallocating small amounts of memory but that not every piece allocated is necessarily deallocated immediately is likely to fragment your heap (particularly on more simplistic heaps). This is undesirable as it makes the heap inefficient and if it becomes sufficiently fragmented then you can find yourself in the situation of being unable to allocate a larger block on memory because although there is more and enough heap space there is not a single block big enough.

                    Comment

                    Working...