By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,650 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,650 IT Pros & Developers. It's quick & easy.

Vector pointer and deallocation

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Andy Chang" <bikejog@@_@@SPAMFREE_FREEhot_baby_mail> wrote in message
news:CY********************@comcast.com...
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
Jul 22 '05 #2

P: n/a
>
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
Jul 22 '05 #3

P: n/a
> 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
Jul 22 '05 #4

P: n/a
"Andy Chang" <bikejog@@_@@SPAMFREE_FREEhot_baby_mail> wrote in message
news:b6********************@comcast.com...

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

Jul 22 '05 #5

P: n/a

"Andy Chang" <bikejog@@_@@SPAMFREE_FREEhot_baby_mail> wrote in message
news:b6********************@comcast.com...

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
Jul 22 '05 #6

P: n/a

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.

Jul 22 '05 #7

P: n/a
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.
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.