STL vectors with uninitialized storage?
Posted
by Jim Hunziker
on Stack Overflow
See other posts from Stack Overflow
or by Jim Hunziker
Published on 2008-09-18T20:31:31Z
Indexed on
2010/05/09
18:58 UTC
Read the original article
Hit count: 223
I'm writing an inner loop that needs to place struct
s in contiguous storage. I don't know how many of these struct
s there will be ahead of time. My problem is that STL's vector
initializes its values to 0, so no matter what I do, I incur the cost of the initialization plus the cost of setting the struct
's members to their values.
Is there any way to prevent the initialization, or is there an STL-like container out there with resizeable contiguous storage and uninitialized elements?
(I'm certain that this part of the code needs to be optimized, and I'm certain that the initialization is a significant cost.)
Also, see my comments below for a clarification about when the initialization occurs.
SOME CODE:
void GetsCalledALot(int* data1, int* data2, int count) {
int mvSize = memberVector.size()
memberVector.resize(mvSize + count); // causes 0-initialization
for (int i = 0; i < count; ++i) {
memberVector[mvSize + i].d1 = data1[i];
memberVector[mvSize + i].d2 = data2[i];
}
}
© Stack Overflow or respective owner