Click here to Skip to main content
16,004,974 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: VC++ 6.0 question newbie question Pin
johnny alpaca5-Mar-08 23:47
johnny alpaca5-Mar-08 23:47 
GeneralRe: VC++ 6.0 question newbie question Pin
toxcct5-Mar-08 3:39
toxcct5-Mar-08 3:39 
QuestionRe: VC++ 6.0 question newbie question Pin
David Crow5-Mar-08 8:40
David Crow5-Mar-08 8:40 
GeneralRe: VC++ 6.0 question newbie question Pin
toxcct5-Mar-08 21:14
toxcct5-Mar-08 21:14 
QuestionRe: VC++ 6.0 question newbie question Pin
David Crow6-Mar-08 4:18
David Crow6-Mar-08 4:18 
GeneralRe: VC++ 6.0 question newbie question Pin
toxcct6-Mar-08 4:20
toxcct6-Mar-08 4:20 
Generalredundant allocation Pin
George_George5-Mar-08 2:45
George_George5-Mar-08 2:45 
GeneralRe: redundant allocation [modified] Pin
toxcct5-Mar-08 4:10
toxcct5-Mar-08 4:10 
the problem is how deque and vector manage their own internal buffers.

the way vector allocates its memory is like this: if you create an empty vector, it automatically reserves the amount of memory for 10 elements (if I remember well). vector has another constructor to tell it another starting reserved memory.

if you never use reserve(), vector will manage its internal buffer alone, and you have to know that its size doesn't increase constantly.
for example (and that's just an example - it may differ from implementations):
class T {};
 
std::vector<T> v = vector<T>();    //capacity = 5, lenght = 0
v.pushback(T());                   //capacity = 5, lenght = 1
v.pushback(T());                   //capacity = 5, lenght = 2
v.pushback(T());                   //capacity = 5, lenght = 3
v.pushback(T());                   //capacity = 5, lenght = 4
v.pushback(T());                   //capacity = 5, lenght = 5
v.pushback(T());                   //capacity = 10, lenght = 6
v.pushback(T());                   //capacity = 10, lenght = 7
v.pushback(T());                   //capacity = 10, lenght = 8
v.pushback(T());                   //capacity = 10, lenght = 9
v.pushback(T());                   //capacity = 10, lenght = 10

as you see, vector has a buffer which doesn't always grow when inserting a new element.
the advantage of this is that it ensures that all the elements in the vector are adjacent.
the inconvenient is that when you insert or delete elements in the middle of the container, it has to move every other elements after the position you insert/delete, thus reallocation.

deque work in a totally different way.
all elements are not adjacent in memory. deque is a linked list, that means each element is somewhere in memory, and has a pointer to the next (and previous) element. so, when you insert an element in a deque, you just have to new it, and change the pointers to incorporate the new element.
same with deletion. you just have to change the pointers of the previous (and next) elements to point now on each others, jumping the element we're deleting...

so, with a deque, the length() is always the allocated amount of memory, that why the writer said that "none of the allocations are redundant"...


modified on Thursday, March 6, 2008 3:16 AM

GeneralRe: redundant allocation Pin
George_George5-Mar-08 18:29
George_George5-Mar-08 18:29 
GeneralRe: redundant allocation Pin
toxcct5-Mar-08 21:16
toxcct5-Mar-08 21:16 
GeneralRe: redundant allocation Pin
George_George5-Mar-08 21:39
George_George5-Mar-08 21:39 
GeneralTruncate a number with decimals Pin
piul5-Mar-08 1:57
piul5-Mar-08 1:57 
GeneralRe: Truncate a number with decimals Pin
Cedric Moonen5-Mar-08 2:02
Cedric Moonen5-Mar-08 2:02 
GeneralRe: Truncate a number with decimals Pin
piul5-Mar-08 2:06
piul5-Mar-08 2:06 
GeneralRe: Truncate a number with decimals Pin
CPallini5-Mar-08 2:13
mveCPallini5-Mar-08 2:13 
GeneralRe: Truncate a number with decimals Pin
CPallini5-Mar-08 2:09
mveCPallini5-Mar-08 2:09 
GeneralRe: Truncate a number with decimals Pin
piul5-Mar-08 2:14
piul5-Mar-08 2:14 
GeneralRe: Truncate a number with decimals Pin
CPallini5-Mar-08 2:17
mveCPallini5-Mar-08 2:17 
GeneralRe: Truncate a number with decimals Pin
Eric Pruneau5-Mar-08 5:33
Eric Pruneau5-Mar-08 5:33 
GeneralProblem with Release Mode Pin
ritz12345-Mar-08 1:49
ritz12345-Mar-08 1:49 
GeneralRe: Problem with Release Mode Pin
Cedric Moonen5-Mar-08 2:00
Cedric Moonen5-Mar-08 2:00 
GeneralRe: Problem with Release Mode Pin
CPallini5-Mar-08 2:00
mveCPallini5-Mar-08 2:00 
GeneralRe: Problem with Release Mode Pin
_AnsHUMAN_ 5-Mar-08 2:01
_AnsHUMAN_ 5-Mar-08 2:01 
QuestionHow to get the username associated with a process using win32 apis Pin
vineeshV5-Mar-08 1:24
vineeshV5-Mar-08 1:24 
AnswerRe: How to get the username associated with a process using win32 apis Pin
Rajkumar R5-Mar-08 3:11
Rajkumar R5-Mar-08 3:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.