Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Pre allocation of memory
Forum Updated to NodeBB v4.3 + New Features

Pre allocation of memory

Scheduled Pinned Locked Moved C++ Gurus
18 Posts 8 Posters 11.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    giesbert
    wrote on last edited by
    #9

    An std::vector (and also a QVector) is a C - array. SO if you have 20 elements in and need to reallocate the vector, the old content MUST be moved in memory, so pointers to the old (unchanged) array would definitly get invalid.

    If you can preallocate the vector and do not resize it, than you can also give pointers out of it. Think of it like a C-style array. If you have no space left and want to add elements, you have to move all elements to a new array which is bigger.

    Both vectors have just a bit more comfort, like moving, adding, preallocating, but in general, they are C-style arrays.

    Nokia Certified Qt Specialist.
    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Taamalus
      wrote on last edited by
      #10

      Not sure if this helps, since I don't like arrays, never used them and would not know how they compare to a vector, just giving another angle to the discussion.

      I think a std::vector is contiguous through out its life. When the allocated memory block for a vector is exhausted during a growth, the entire vector gets copied into a larger block, and whatever is contained gets copied with it. Any type of data can be accessed in this vector as before the copy took place.

      The catch is when the vector is created at run time and is filled with a horde of pointers and needs to be destructed, than all those pointers in the vectors need to be destroyed as well and at programming time you don't know which one's will be created and where - you are stuck or it's time for smart pointers.

      Bottom line? I practise and test stuff to the hilt. If the vector works the way I want it, it's end of story.

      ... time waits for no one. - Henry

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Panke
        wrote on last edited by
        #11

        Preallocating may give you an advantage in writing exception save code.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cazador7907
          wrote on last edited by
          #12

          I believe that I understand now. Coming from the Visual Basic and C# universes, I was shielded (to an extent) from this sort of thing. Is there a way to check if a pointer assignment is still valid?

          Laurence -

          Laurence -

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Taamalus
            wrote on last edited by
            #13

            @ cazador7907
            That is more or less a save pointer.
            Hopefully I will not get shot down showing a link to the competition, but I learned so much from those guys.

            http://www.codeguru.com/cpp/cpp/article.php/c17775/The-Smart-Pointer-That-Makes-Your-C-Applications-Safer---stduniqueptr.htm

            ... time waits for no one. - Henry

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Panke
              wrote on last edited by
              #14

              But unique_ptr ist no solution here. A unique_ptr implies that you own the object pointed to and
              that the lifetime of the object and the unique_ptr are (more or less) identical. So you should know if its valid, regardless of the use of unique_ptr.

              In fact there is no way no ensure, that a pointer points to an object that has not been deleted.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                Taamalus
                wrote on last edited by
                #15

                My humblest apologies.
                The pointer style in my previous link does work in std::vectors, and those vectors that had been moved(copied). The topic is about effectively using vectors, and I added information to that.

                And sure, no smart/save pointer is a direct answer to cazador7907 question, but the topic is about vectors; that's why I wrote 'That is more or less ...' , I assumed his/her question was in context to the topic, since we all agreed that to allocate space for a vector is a good idea, and what left to figgure out is what to do with its contents. :)

                ... time waits for no one. - Henry

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #16

                  In general, there is no platform independant method to check, whether a pure pointer is valid or not.

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #17

                    Like Gerolf said: there is no general way. But, if you are dealing with a QObject you're pointing to, you can use a QPointer. That pointer will reset itself to 0 if the object is deleted.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      giesbert
                      wrote on last edited by
                      #18

                      But that only helps if you are deleting the objects.

                      I think the intention was:

                      using a QVector which is storing objects and using pointers on those objects. And there it definitly does not work.

                      Nokia Certified Qt Specialist.
                      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved