Vector pointer and deallocation

Discussion in 'C++' started by Andy Chang, Jan 4, 2005.

  1. Andy Chang

    Andy Chang Guest

    Hi,
    If I have

    typedef struct {
    std::vector <float> vSomeVal;
    int i;
    } VEC_T;

    Then is VEC_T * ptr a valid pointer to the struct accessing the vector?
    Also do I need to explicitly deallocate the vector by calling the clear()
    function or does the vector has its own destructor?

    Thanks
    Andy
    Andy Chang, Jan 4, 2005
    #1
    1. Advertising

  2. Andy Chang

    Mike Wahler Guest

    "Andy Chang" <bikejog@@_@@SPAMFREE_FREEhot_baby_mail> wrote in message
    news:...
    > Hi,
    > If I have
    >
    > typedef struct {
    > std::vector <float> vSomeVal;
    > int i;
    > } VEC_T;


    Why the typedef?

    >
    > Then is VEC_T * ptr a valid pointer to the struct accessing the

    vector?

    The statement:

    VEC_T *ptr;

    declares a pointer to your 'VEC_T' type, but it's not valid
    until you assign it the address of a type 'VEC_T' object.

    > Also do I need to explicitly deallocate the vector by calling the clear()


    'clear()' has nothing to do with allocation/deallocation
    of the vector. It erases all the vector's elements (I.e.
    makes it 'empty').

    > function or does the vector has its own destructor?


    All the standard library containers have their own destructors
    which take care of 'cleanup'.

    Which C++ book(s) are you reading?

    -Mike
    Mike Wahler, Jan 4, 2005
    #2
    1. Advertising

  3. Andy Chang

    Andy Chang Guest

    >
    > Why the typedef?
    >


    Is there something wrong with using typedef?

    > The statement:
    >
    > VEC_T *ptr;
    >
    > declares a pointer to your 'VEC_T' type, but it's not valid
    > until you assign it the address of a type 'VEC_T' object.
    >


    When I asked whether it's valid, I meant is legal within C++ to declare
    such pointers.

    > 'clear()' has nothing to do with allocation/deallocation
    > of the vector. It erases all the vector's elements (I.e.
    > makes it 'empty').
    >
    >> function or does the vector has its own destructor?

    >
    > All the standard library containers have their own destructors
    > which take care of 'cleanup'.
    >

    Good. So all the elements I .push_back(item) into the vector will
    automatically get cleaned up after the vector falls out of scope?

    > Which C++ book(s) are you reading?
    >


    I'm reading "C++ Primer" by Standley B. Lippman, 2nd Edition. I'm
    relatively new to C++ and vectors in particular.


    Andy
    Andy Chang, Jan 4, 2005
    #3
  4. Andy Chang

    Stephen Howe Guest

    > When I asked whether it's valid, I meant is legal within C++ to declare
    > such pointers.


    Yes

    > Good. So all the elements I .push_back(item) into the vector will
    > automatically get cleaned up after the vector falls out of scope?


    Yes

    Stephen Howe
    Stephen Howe, Jan 4, 2005
    #4
  5. Andy Chang

    Paul Guest

    "Andy Chang" <bikejog@@_@@SPAMFREE_FREEhot_baby_mail> wrote in message
    news:...
    > >
    > > Why the typedef?
    > >

    >
    > Is there something wrong with using typedef?


    It isn't necessary in a C++ program to declare structs using "typedef":

    struct Whatever
    {
    // your members here
    };

    This is much clearer and more "natural" to a C++ programmer. Not only that
    "typedef struct" in a C++ program is a dead-giveaway that either you're a
    'C' programmer that hasn't shaken the habit, or you're reading or learning
    from 'C' code and/or books.

    The only good reason I see for "typedef struct" in a C++ program is if you
    are declaring your struct in a header file that will be used in both C and
    C++ programs. However since you are declaring a std::vector in your struct,
    the header can't be used in a 'C' program, so it's a moot point.

    Paul
    >
    > > The statement:
    > >
    > > VEC_T *ptr;
    > >
    > > declares a pointer to your 'VEC_T' type, but it's not valid
    > > until you assign it the address of a type 'VEC_T' object.
    > >

    >
    > When I asked whether it's valid, I meant is legal within C++ to

    declare
    > such pointers.
    >
    > > 'clear()' has nothing to do with allocation/deallocation
    > > of the vector. It erases all the vector's elements (I.e.
    > > makes it 'empty').
    > >
    > >> function or does the vector has its own destructor?

    > >
    > > All the standard library containers have their own destructors
    > > which take care of 'cleanup'.
    > >

    > Good. So all the elements I .push_back(item) into the vector will
    > automatically get cleaned up after the vector falls out of scope?
    >
    > > Which C++ book(s) are you reading?
    > >

    >
    > I'm reading "C++ Primer" by Standley B. Lippman, 2nd Edition. I'm
    > relatively new to C++ and vectors in particular.
    >
    >
    > Andy
    >
    >
    Paul, Jan 4, 2005
    #5
  6. Andy Chang

    Mike Wahler Guest

    "Andy Chang" <bikejog@@_@@SPAMFREE_FREEhot_baby_mail> wrote in message
    news:...
    > >
    > > Why the typedef?
    > >

    >
    > Is there something wrong with using typedef?


    It's not 'wrong', but not necessary.
    When you write:

    struct X
    {
    /* etc */
    };

    You can refer to this type by simply writing:

    X an_object;

    >
    > > The statement:
    > >
    > > VEC_T *ptr;
    > >
    > > declares a pointer to your 'VEC_T' type, but it's not valid
    > > until you assign it the address of a type 'VEC_T' object.
    > >

    >
    > When I asked whether it's valid, I meant is legal within C++ to

    declare
    > such pointers.


    Yes, it is.

    >
    > > 'clear()' has nothing to do with allocation/deallocation
    > > of the vector. It erases all the vector's elements (I.e.
    > > makes it 'empty').
    > >
    > >> function or does the vector has its own destructor?

    > >
    > > All the standard library containers have their own destructors
    > > which take care of 'cleanup'.
    > >

    > Good. So all the elements I .push_back(item) into the vector will
    > automatically get cleaned up after the vector falls out of scope?


    Yes.

    >
    > > Which C++ book(s) are you reading?
    > >

    >
    > I'm reading "C++ Primer" by Standley B. Lippman, 2nd Edition.


    That's a pretty good book, but I think it's a bit dated. I think that one
    is in the 3rd or 4th edition now. If it was published much before
    1997, it will not likely reflect the current form of C++.

    >I'm
    > relatively new to C++ and vectors in particular.


    Good luck with your studies.

    -Mike
    Mike Wahler, Jan 4, 2005
    #6
  7. Andy Chang

    owl ling Guest


    > Why the typedef?
    > Is there something wrong with using typedef?

    No, It isn't wrong, but you was writting c++ prorams in c style,
    I don't think it is a good habit.
    owl ling, Jan 4, 2005
    #7
  8. Andy Chang

    Ron Natalie Guest

    Andy Chang wrote:
    >>Why the typedef?
    >>

    >
    >
    > Is there something wrong with using typedef?
    >


    It's unnecessary...struct tags in C++ are types:
    struct VEC_T {
    ...
    };

    >
    > Good. So all the elements I .push_back(item) into the vector will
    > automatically get cleaned up after the vector falls out of scope?
    >


    Scope is not the right word. The vector elements will be destroyed when
    the vector is destroyed which will be destroyed when the VEC_T object is
    destroyed. Scope applies to names rather than object lifetimes.

    int foo() {
    VEC_T* p = new VEC_T;

    p->vSomeVal.push_back(4.0);

    }

    Here while the pointer's lifetime ends at the end of the function, the VEC_T
    object allocated has dynamic scope and continues to exist until deleted.
    Ron Natalie, Jan 5, 2005
    #8
    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. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  2. madhu
    Replies:
    6
    Views:
    620
    madhu
    Nov 13, 2006
  3. Kumar McMillan
    Replies:
    0
    Views:
    408
    Kumar McMillan
    Apr 19, 2007
  4. Replies:
    18
    Views:
    729
  5. Cristiano

    vector and struct deallocation

    Cristiano, Sep 1, 2012, in forum: C++
    Replies:
    28
    Views:
    769
    Jorgen Grahn
    Sep 4, 2012
Loading...

Share This Page