Why does std::vector uses std::allocator instead of operator new and delete
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
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++
add a comment |Â
up vote
7
down vote
favorite
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++
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
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
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++
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++
c++
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
add a comment |Â
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
add a comment |Â
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:
- Existing objects in the vector which needs to be moved to new space. They are initialized data.
- Extra reserved space which doesn't store any object yet and thus uninitialized.
add a comment |Â
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:
- Existing objects in the vector which needs to be moved to new space. They are initialized data.
- Extra reserved space which doesn't store any object yet and thus uninitialized.
add a comment |Â
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:
- Existing objects in the vector which needs to be moved to new space. They are initialized data.
- Extra reserved space which doesn't store any object yet and thus uninitialized.
add a comment |Â
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:
- Existing objects in the vector which needs to be moved to new space. They are initialized data.
- Extra reserved space which doesn't store any object yet and thus uninitialized.
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:
- Existing objects in the vector which needs to be moved to new space. They are initialized data.
- Extra reserved space which doesn't store any object yet and thus uninitialized.
edited 20 mins ago
answered 31 mins ago
taskinoor
38.2k794122
38.2k794122
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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