Why does std::vector uses std::allocator instead of operator new and delete

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite
1












I'm reading the book "Programming Principle and Practices using C++" by the author of the language.



I'm reading to the part where the book basically describe how to implement the std::vector. And here's a piece of code in the book:



template<typename T, typename A = std::allocator<T>> class vector 
A alloc;
int space;
T* elem;
//...some code
reserve(int newalloc)
if (newalloc <= space) return; // never decrease allocation
T* p = alloc.allocate(newalloc); // allocate new space
for (int i = 0; i < sz; ++i) alloc.construct(&p[i], elem[i]); // copy
for (int i = 0; i < sz; ++i) alloc.destroy(&elem[i]); // destroy
alloc.deallocate(elem, space); // deallocate old space
elem = p;
space = newalloc;

;


The book mentions that we have to use std::allocator because the vector's data structure consists some initialized data and some uninitialized data.



I'm not clear what that means. What could go wrong if I use new and delete?



template<typename T> class vector2 
A alloc;
int space;
T* elem;
//some code
reserve(int newalloc)
if (newalloc <= space) return;
T* p = new T[newallow];
for (int i = 0; i < sz; ++i) p[i] = elem[i];
for (int i = 0; i < sz; ++i) delete elem;
elem = p;
space = newalloc;

;









share|improve this question





















  • Possible duplicate of stackoverflow.com/questions/31358804/…
    – P.W
    17 mins ago










  • Possible duplicate of What's the advantage of using std::allocator instead of new in C++?
    – jww
    1 min ago















up vote
7
down vote

favorite
1












I'm reading the book "Programming Principle and Practices using C++" by the author of the language.



I'm reading to the part where the book basically describe how to implement the std::vector. And here's a piece of code in the book:



template<typename T, typename A = std::allocator<T>> class vector 
A alloc;
int space;
T* elem;
//...some code
reserve(int newalloc)
if (newalloc <= space) return; // never decrease allocation
T* p = alloc.allocate(newalloc); // allocate new space
for (int i = 0; i < sz; ++i) alloc.construct(&p[i], elem[i]); // copy
for (int i = 0; i < sz; ++i) alloc.destroy(&elem[i]); // destroy
alloc.deallocate(elem, space); // deallocate old space
elem = p;
space = newalloc;

;


The book mentions that we have to use std::allocator because the vector's data structure consists some initialized data and some uninitialized data.



I'm not clear what that means. What could go wrong if I use new and delete?



template<typename T> class vector2 
A alloc;
int space;
T* elem;
//some code
reserve(int newalloc)
if (newalloc <= space) return;
T* p = new T[newallow];
for (int i = 0; i < sz; ++i) p[i] = elem[i];
for (int i = 0; i < sz; ++i) delete elem;
elem = p;
space = newalloc;

;









share|improve this question





















  • Possible duplicate of stackoverflow.com/questions/31358804/…
    – P.W
    17 mins ago










  • Possible duplicate of What's the advantage of using std::allocator instead of new in C++?
    – jww
    1 min ago













up vote
7
down vote

favorite
1









up vote
7
down vote

favorite
1






1





I'm reading the book "Programming Principle and Practices using C++" by the author of the language.



I'm reading to the part where the book basically describe how to implement the std::vector. And here's a piece of code in the book:



template<typename T, typename A = std::allocator<T>> class vector 
A alloc;
int space;
T* elem;
//...some code
reserve(int newalloc)
if (newalloc <= space) return; // never decrease allocation
T* p = alloc.allocate(newalloc); // allocate new space
for (int i = 0; i < sz; ++i) alloc.construct(&p[i], elem[i]); // copy
for (int i = 0; i < sz; ++i) alloc.destroy(&elem[i]); // destroy
alloc.deallocate(elem, space); // deallocate old space
elem = p;
space = newalloc;

;


The book mentions that we have to use std::allocator because the vector's data structure consists some initialized data and some uninitialized data.



I'm not clear what that means. What could go wrong if I use new and delete?



template<typename T> class vector2 
A alloc;
int space;
T* elem;
//some code
reserve(int newalloc)
if (newalloc <= space) return;
T* p = new T[newallow];
for (int i = 0; i < sz; ++i) p[i] = elem[i];
for (int i = 0; i < sz; ++i) delete elem;
elem = p;
space = newalloc;

;









share|improve this question













I'm reading the book "Programming Principle and Practices using C++" by the author of the language.



I'm reading to the part where the book basically describe how to implement the std::vector. And here's a piece of code in the book:



template<typename T, typename A = std::allocator<T>> class vector 
A alloc;
int space;
T* elem;
//...some code
reserve(int newalloc)
if (newalloc <= space) return; // never decrease allocation
T* p = alloc.allocate(newalloc); // allocate new space
for (int i = 0; i < sz; ++i) alloc.construct(&p[i], elem[i]); // copy
for (int i = 0; i < sz; ++i) alloc.destroy(&elem[i]); // destroy
alloc.deallocate(elem, space); // deallocate old space
elem = p;
space = newalloc;

;


The book mentions that we have to use std::allocator because the vector's data structure consists some initialized data and some uninitialized data.



I'm not clear what that means. What could go wrong if I use new and delete?



template<typename T> class vector2 
A alloc;
int space;
T* elem;
//some code
reserve(int newalloc)
if (newalloc <= space) return;
T* p = new T[newallow];
for (int i = 0; i < sz; ++i) p[i] = elem[i];
for (int i = 0; i < sz; ++i) delete elem;
elem = p;
space = newalloc;

;






c++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 38 mins ago









namcao00

774




774











  • Possible duplicate of stackoverflow.com/questions/31358804/…
    – P.W
    17 mins ago










  • Possible duplicate of What's the advantage of using std::allocator instead of new in C++?
    – jww
    1 min ago

















  • Possible duplicate of stackoverflow.com/questions/31358804/…
    – P.W
    17 mins ago










  • Possible duplicate of What's the advantage of using std::allocator instead of new in C++?
    – jww
    1 min ago
















Possible duplicate of stackoverflow.com/questions/31358804/…
– P.W
17 mins ago




Possible duplicate of stackoverflow.com/questions/31358804/…
– P.W
17 mins ago












Possible duplicate of What's the advantage of using std::allocator instead of new in C++?
– jww
1 min ago





Possible duplicate of What's the advantage of using std::allocator instead of new in C++?
– jww
1 min ago













1 Answer
1






active

oldest

votes

















up vote
8
down vote



accepted











What could go wrong if I use new and delete?




T* p = new T[newallow];


One reason is that this won't compile if T doesn't have default constructor.



The basic idea of allocator is to separate the steps of allocating memory and object construction. Default new combines the both. In case of vector reserve we only want to allocate the required memory. We can't construct or initialize the objects at that time since the type may not be default constructible. The objects can be constructed later only when we pass the objects to store in some other operation, e.g.



v[i] = myObj;


This can't be achieved without separating memory allocation and object construction in two different steps.



Also note that, allocator have advanced usages when someone wants to customize the memory allocation.




The book mentions that we have to use std::allocator because the vector's data structure consists some initialized data and some uninitialized data.




What author meant here is that while growing the capacity by calling reserve we will have two types of data:



  1. Existing objects in the vector which needs to be moved to new space. They are initialized data.

  2. Extra reserved space which doesn't store any object yet and thus uninitialized.





share|improve this answer






















    Your Answer





    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52436643%2fwhy-does-stdvector-uses-stdallocator-instead-of-operator-new-and-delete%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    8
    down vote



    accepted











    What could go wrong if I use new and delete?




    T* p = new T[newallow];


    One reason is that this won't compile if T doesn't have default constructor.



    The basic idea of allocator is to separate the steps of allocating memory and object construction. Default new combines the both. In case of vector reserve we only want to allocate the required memory. We can't construct or initialize the objects at that time since the type may not be default constructible. The objects can be constructed later only when we pass the objects to store in some other operation, e.g.



    v[i] = myObj;


    This can't be achieved without separating memory allocation and object construction in two different steps.



    Also note that, allocator have advanced usages when someone wants to customize the memory allocation.




    The book mentions that we have to use std::allocator because the vector's data structure consists some initialized data and some uninitialized data.




    What author meant here is that while growing the capacity by calling reserve we will have two types of data:



    1. Existing objects in the vector which needs to be moved to new space. They are initialized data.

    2. Extra reserved space which doesn't store any object yet and thus uninitialized.





    share|improve this answer


























      up vote
      8
      down vote



      accepted











      What could go wrong if I use new and delete?




      T* p = new T[newallow];


      One reason is that this won't compile if T doesn't have default constructor.



      The basic idea of allocator is to separate the steps of allocating memory and object construction. Default new combines the both. In case of vector reserve we only want to allocate the required memory. We can't construct or initialize the objects at that time since the type may not be default constructible. The objects can be constructed later only when we pass the objects to store in some other operation, e.g.



      v[i] = myObj;


      This can't be achieved without separating memory allocation and object construction in two different steps.



      Also note that, allocator have advanced usages when someone wants to customize the memory allocation.




      The book mentions that we have to use std::allocator because the vector's data structure consists some initialized data and some uninitialized data.




      What author meant here is that while growing the capacity by calling reserve we will have two types of data:



      1. Existing objects in the vector which needs to be moved to new space. They are initialized data.

      2. Extra reserved space which doesn't store any object yet and thus uninitialized.





      share|improve this answer
























        up vote
        8
        down vote



        accepted







        up vote
        8
        down vote



        accepted







        What could go wrong if I use new and delete?




        T* p = new T[newallow];


        One reason is that this won't compile if T doesn't have default constructor.



        The basic idea of allocator is to separate the steps of allocating memory and object construction. Default new combines the both. In case of vector reserve we only want to allocate the required memory. We can't construct or initialize the objects at that time since the type may not be default constructible. The objects can be constructed later only when we pass the objects to store in some other operation, e.g.



        v[i] = myObj;


        This can't be achieved without separating memory allocation and object construction in two different steps.



        Also note that, allocator have advanced usages when someone wants to customize the memory allocation.




        The book mentions that we have to use std::allocator because the vector's data structure consists some initialized data and some uninitialized data.




        What author meant here is that while growing the capacity by calling reserve we will have two types of data:



        1. Existing objects in the vector which needs to be moved to new space. They are initialized data.

        2. Extra reserved space which doesn't store any object yet and thus uninitialized.





        share|improve this answer















        What could go wrong if I use new and delete?




        T* p = new T[newallow];


        One reason is that this won't compile if T doesn't have default constructor.



        The basic idea of allocator is to separate the steps of allocating memory and object construction. Default new combines the both. In case of vector reserve we only want to allocate the required memory. We can't construct or initialize the objects at that time since the type may not be default constructible. The objects can be constructed later only when we pass the objects to store in some other operation, e.g.



        v[i] = myObj;


        This can't be achieved without separating memory allocation and object construction in two different steps.



        Also note that, allocator have advanced usages when someone wants to customize the memory allocation.




        The book mentions that we have to use std::allocator because the vector's data structure consists some initialized data and some uninitialized data.




        What author meant here is that while growing the capacity by calling reserve we will have two types of data:



        1. Existing objects in the vector which needs to be moved to new space. They are initialized data.

        2. Extra reserved space which doesn't store any object yet and thus uninitialized.






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 20 mins ago

























        answered 31 mins ago









        taskinoor

        38.2k794122




        38.2k794122



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52436643%2fwhy-does-stdvector-uses-stdallocator-instead-of-operator-new-and-delete%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Confectionery