Uninitialized vector?

Discussion in 'C++' started by Marcin Kalicinski, Aug 14, 2005.

  1. 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
    Marcin Kalicinski, Aug 14, 2005
    #1
    1. Advertising

  2. Marcin Kalicinski

    Mark P Guest

    Marcin Kalicinski wrote:
    > 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
    Mark P, Aug 14, 2005
    #2
    1. Advertising

  3. Marcin Kalicinski

    Cy Edmunds Guest

    "Marcin Kalicinski" <> wrote in message
    news:x%KLe.9318$...
    > 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 news: the world is better than you think!

    > cheers,
    > Marcin


    --
    Cy
    http://home.rochester.rr.com/cyhome/
    Cy Edmunds, Aug 14, 2005
    #3
  4. "Marcin Kalicinski" <> wrote in message
    news:x%KLe.9318$...

    > 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?
    Andrew Koenig, Aug 15, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. learningjava
    Replies:
    5
    Views:
    2,261
    Miguel De Anda
    Oct 17, 2003
  2. pmatos
    Replies:
    6
    Views:
    23,732
  3. Replies:
    8
    Views:
    1,895
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    547
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    349
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page