strange behaviour of vector::push_back (v5.1)

Developers
Smith
2007-02-15
2013-05-13
  • Smith

    Smith - 2007-02-15

    output of:
    ---
    #include <vector>
    #include <stdio.h>

    struct A {
      A ()         { printf ("A::A\n"); }
    ~A ()         { printf ("A::~A\n"); }
      A (const A&) { printf ("copy A::A\n"); }
    };

    int main () {
      {
        std::vector<A> list;
        list.push_back (A());
      }
      return 0;
    }
    ---
    is

    A::A
    A::~A
    A::~A

    i.e. ctor/dctor are not paired...
    Where is an error?
    Is there are 'trivial_copy' bug?

    P.S. the copy ctor will be invoked after adding 'operator=' to struct A, is it right?

     
    • Petr Ovtchenkov

      Petr Ovtchenkov - 2007-02-15

      No bugs. See Standard, 12.8 (or other book about C++).

       
      • Smith

        Smith - 2007-02-15

        Mmmm... Sorry again, the standard says, that
        implementation "is allowed to omit the copy construction of a class object,
        even if the copy constructor and/or destructor for the object have side effects."

        But why we have two dctor calls?
        (see footnote '111')

         
        • Petr Ovtchenkov

          Petr Ovtchenkov - 2007-02-15

          > But why we have two dctor calls?

          one from temporary obj [A()], one from dtor of vector's content.

           
          • Smith

            Smith - 2007-02-15

            sorry, but I don't understand.

            Standard says that implementation may omit copy ctor
            invocation and two (or more) references may look at
            one object, but standard says no that ctor/dctor may
            not be paired: "Because only one object is destroyed
            instead of two, and one copy constructor is not executed,
            there is still one object destroyed for each one constructed"

            next code bring to corruption of the heap (VC8):
            ---
            #include <vector>
            #include <stdio.h>

            struct A {
             
              A () : x (new int[5]) {
                printf ("A::A\n");
              }

              A (const A&) : x (new int [5]) {
                printf ("copy A::A\n");
              }

              ~A () {
                delete [] x;
                printf ("A::~A\n");
              }

              int* x; 
            };

            int main () {
              {
                std::vector<A> list;
                list.push_back (A());
              }
              return 0;
            }
            ---

            what about that?

             
            • Petr Ovtchenkov

              Petr Ovtchenkov - 2007-02-15

              1. This isn't STLport issue. Choose proper place for questions.
              2. On assignment operator you copy pointer x, then delete it once, and then delete it once more. On second delete you have crash.

               
            • Smith

              Smith - 2007-02-15

              Sorry again, I understood this optimization,
              (there are error in my previous code expample:
              class have no 'operator =')

              Thank you.

               
    • Smith

      Smith - 2007-02-15

      Oh, yes, 12.8.15:

      When certain criteria are met, an implementation is allowed to omit the copy construction of a class object, even if the copy constructor and/or destructor for the object have side effects.

      Sorry.

       

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks