Uninitialized vector?

M

Marcin Kalicinski

int a; // #1 uninitialized value
int a(0) // #2 zero-initialized value
std::vector<int> v(10); // #3 zero-initialized values (why?)
std::vector<int> v(10, 0); // #4 zero-initialized values

How do I construct a vector of ints without initializing them to zero? Why
is #3 zero-initializing the ints? This is unintutitive, unexpected and slow.
If one wanted to intialize he would use syntax #4.

World would be better if int() created uninitialized value.

cheers,
Marcin
 
M

Mark P

Marcin said:
int a; // #1 uninitialized value
int a(0) // #2 zero-initialized value
std::vector<int> v(10); // #3 zero-initialized values (why?)
std::vector<int> v(10, 0); // #4 zero-initialized values

How do I construct a vector of ints without initializing them to zero?

You can't, short of providing some other initialization value instead.
Why is #3 zero-initializing the ints?

Because vectors may hold arbitrary objects not just PODs and it's not
very sensible to initialize general objects with garbage memory. They
need to be default constructed if not explicitly constructed.
This is unintutitive, unexpected and slow.
If one wanted to intialize he would use syntax #4.

I find it to be neither unintuitive nor unexpected. As for slow that's
debatable (though I wouldn't assume it until proven so) but also
avoidable if it's really a concern. You can use an array. Or you can
use a vector and reserve the required amount of space without putting
anything into it.
World would be better if int() created uninitialized value.

Maybe, but though it's not obvious how much you gain from this here
since vector will still have to construct an int.
cheers,
Marcin

Mark
 
C

Cy Edmunds

Marcin Kalicinski said:
int a; // #1 uninitialized value
int a(0) // #2 zero-initialized value
std::vector<int> v(10); // #3 zero-initialized values (why?)
std::vector<int> v(10, 0); // #4 zero-initialized values

How do I construct a vector of ints without initializing them to zero?

std::vector int v;
v.reserve(10);

However, v.size() will still return 0 and v.at(0) will still throw. In other
words, you have allocated at least enough room for 10 integers without
initializing but the vector is still of length 0. You can write 10 values to
v without any reallocation using v.push_back or v.insert. Then you will have
what you are looking for.
Why is #3 zero-initializing the ints? This is unintutitive, unexpected and
slow.

If you want a collection class for objects and a separate collection class
for each built in type you should try Java. C++ collection classes work on
both built in types and user defined types (with some restrictions).
If one wanted to intialize he would use syntax #4.

World would be better if int() created uninitialized value.

Good the world is better than you think!
 
A

Andrew Koenig

World would be better if int() created uninitialized value.

Why do you think so? If that were the case, then

int x = int();

would be undefined behavior, because it would attempt to copy an
uninitialized value. For that matter, I can't think of any context in which
it would be well-defined to use int(), so why bother allowing it at all?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top