Vector pointer and deallocation

A

Andy Chang

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
 
M

Mike Wahler

Andy Chang said:
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
 
A

Andy Chang

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').


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
 
S

Stephen Howe

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
 
P

Paul

Andy Chang said:
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
 
M

Mike Wahler

Andy Chang said:
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;
When I asked whether it's valid, I meant is legal within C++ to declare
such pointers.

Yes, it is.
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.


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
 
O

owl ling

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.
 
R

Ron Natalie

Andy said:
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.
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top