vector question

Discussion in 'C++' started by Babis Haldas, Jun 19, 2006.

  1. Babis Haldas

    Babis Haldas Guest

    Can you please tell me whats wrong with the follow code
    Its crasing in the line
    cout << v[0]->x1 << endl;
    when accesing the first element of the vector
    If i define the vector as
    vector<foo*>v ;
    everything is ok

    why it doesnt like v(10) ?

    thanks

    #include <iostream>
    #include <vector>

    using namespace std;

    struct foo {
    int x1;
    int x2;
    };

    int main()
    {
    vector<foo*> v(10);
    foo * p;
    p = new foo;
    p->x1 = 111;
    p->x2 = 222;
    v.push_back(p);

    cout << v[0]->x1 << endl;
    }
     
    Babis Haldas, Jun 19, 2006
    #1
    1. Advertising

  2. Babis Haldas wrote:
    > Can you please tell me whats wrong with the follow code
    > Its crasing in the line
    > cout << v[0]->x1 << endl;
    > when accesing the first element of the vector
    > If i define the vector as
    > vector<foo*>v ;
    > everything is ok
    >
    > why it doesnt like v(10) ?


    Because if you don't specify the size, it creates an *empty* vector,
    in which you later insert the *real* (non-null) element, which you
    later access.

    >
    > thanks
    >
    > #include <iostream>
    > #include <vector>
    >
    > using namespace std;
    >
    > struct foo {
    > int x1;
    > int x2;
    > };
    >
    > int main()
    > {
    > vector<foo*> v(10);


    That creates a ten-sized vector of pointers to 'foo', each null (IIRC).

    > foo * p;
    > p = new foo;
    > p->x1 = 111;
    > p->x2 = 222;
    > v.push_back(p);


    That inserts *yet another* pointer to 'foo' at the end of the vector.

    >
    > cout << v[0]->x1 << endl;


    v[0] is a null pointer. You try dereferencing it. Undefined behaviour.

    > }


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 19, 2006
    #2
    1. Advertising

  3. Babis Haldas

    Babis Haldas Guest

    Thanks

    "Victor Bazarov" <> wrote in message
    news:e76jmo$lgd$...
    > Babis Haldas wrote:
    >> Can you please tell me whats wrong with the follow code
    >> Its crasing in the line
    >> cout << v[0]->x1 << endl;
    >> when accesing the first element of the vector
    >> If i define the vector as
    >> vector<foo*>v ;
    >> everything is ok
    >>
    >> why it doesnt like v(10) ?

    >
    > Because if you don't specify the size, it creates an *empty* vector,
    > in which you later insert the *real* (non-null) element, which you
    > later access.
    >
    >>
    >> thanks
    >>
    >> #include <iostream>
    >> #include <vector>
    >>
    >> using namespace std;
    >>
    >> struct foo {
    >> int x1;
    >> int x2;
    >> };
    >>
    >> int main()
    >> {
    >> vector<foo*> v(10);

    >
    > That creates a ten-sized vector of pointers to 'foo', each null (IIRC).
    >
    >> foo * p;
    >> p = new foo;
    >> p->x1 = 111;
    >> p->x2 = 222;
    >> v.push_back(p);

    >
    > That inserts *yet another* pointer to 'foo' at the end of the vector.
    >
    >>
    >> cout << v[0]->x1 << endl;

    >
    > v[0] is a null pointer. You try dereferencing it. Undefined behaviour.
    >
    >> }

    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
    >
     
    Babis Haldas, Jun 19, 2006
    #3
  4. Babis Haldas

    utab Guest


    > > #include <iostream>
    > > #include <vector>
    > >
    > > using namespace std;
    > >
    > > struct foo {
    > > int x1;
    > > int x2;
    > > };
    > >
    > > int main()
    > > {
    > > vector<foo*> v(10);

    >
    > That creates a ten-sized vector of pointers to 'foo', each null (IIRC).


    Why are these pointers are all NULL. Is that because of the ctors of
    the vector class value-initializes them. If that is, is not value
    initializating to 0(NULL for pointers) wrong because it is not a
    built-in type?

    Or I am mistaken at some point?

    Regards,
     
    utab, Jun 19, 2006
    #4
  5. Babis Haldas

    Mark P Guest

    utab wrote:
    >>> #include <iostream>
    >>> #include <vector>
    >>>
    >>> using namespace std;
    >>>
    >>> struct foo {
    >>> int x1;
    >>> int x2;
    >>> };
    >>>
    >>> int main()
    >>> {
    >>> vector<foo*> v(10);

    >> That creates a ten-sized vector of pointers to 'foo', each null (IIRC).

    >
    > Why are these pointers are all NULL. Is that because of the ctors of
    > the vector class value-initializes them. If that is, is not value
    > initializating to 0(NULL for pointers) wrong because it is not a
    > built-in type?
    >


    The vector ctor will default initialize them. For pointers, default
    initialization means that they are set to zero. (This should not be
    confused with other situations where uninitialized variables are given
    garbage values. Default initialized is not the same as uninitialized.)

    Mark
     
    Mark P, Jun 20, 2006
    #5
    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. BCC
    Replies:
    12
    Views:
    846
    Clark Cox
    Feb 6, 2004
  2. pmatos
    Replies:
    6
    Views:
    24,136
  3. Replies:
    8
    Views:
    2,007
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    629
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    400
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page