pointer to a vector

A

A

There are 2 vectors each one having a structure

struct MyStruct
{
int a;
std::string b;
}

std::vector<MyStruct> v1;
std::vector<MyStruct> v2;

Now I want a pointer to which one I will use... v1 or v2

std::vector<MyStruct> *v = (condition)? &v1 : &v2;

Finally I access it using:

for (unsigned i = 0; i < v->size(); i++)
{
v->operator[](i).a = i + 1;
}

My lack of understanding here is:

a) does the above *v needs to be deleted? Isn't it just a pointer variable?
Or it works differently when it points to a vector? Just to be clear, I
don't actually need to delete v1 or v2. I just need to cleanup *v if
required.

b) what's the heap or stack or difference or advantage of them in relation
to the above?
 
A

A

Actually I just found that I can use *v like this:

(*v).a = i + 1;

Cleaner to me than

v->operator[](i).a = i + 1;

However, my questions are still the same.
 
J

Jorgen Grahn

There are 2 vectors each one having a structure

struct MyStruct
{
int a;
std::string b;
}

std::vector<MyStruct> v1;
std::vector<MyStruct> v2;

Now I want a pointer to which one I will use... v1 or v2

std::vector<MyStruct> *v = (condition)? &v1 : &v2;

You don't need a pointer below -- a reference would have worked just
as well, and the syntax would have been cleaner.
Finally I access it using:

for (unsigned i = 0; i < v->size(); i++)
{
v->operator[](i).a = i + 1;
}

My lack of understanding here is:

a) does the above *v needs to be deleted? Isn't it just a pointer variable?

No, and yes. You haven't done 'new', so you're not responsible for
doing 'delete'. BTW, both of those are rarely needed in modern code
-- if you use them a lot you're probably doing something wrong.
Or it works differently when it points to a vector? Just to be clear, I
don't actually need to delete v1 or v2. I just need to cleanup *v if
required.

No cleanup required. But I don't really understand what you're trying
to say ...
b) what's the heap or stack or difference or advantage of them in relation
to the above?

I don't understand that question. Please rephrase.

/Jorgen
 
A

A

Jorgen Grahn said:
No, and yes. You haven't done 'new', so you're not responsible for
doing 'delete'. BTW, both of those are rarely needed in modern code
-- if you use them a lot you're probably doing something wrong.

No, I don't use normally new or delete. I use boost smart pointers and
vectors.
Good thing to know this rule of a thumb - using new = delete, not using it,
then it is just a pointer.
No cleanup required. But I don't really understand what you're trying
to say ...

What I though is that if it is pointer to a vector then it is somehow
different but obviously it is just a same pointer like any other. So yes,
then no cleanup is required, it is just a pointer variable.
I don't understand that question. Please rephrase.

A guy here was mentioning heap so I though it was related. Seems it is not:
http://facepunch.com/showthread.php?t=780439
 
A

A

Paavo Helde said:
No, as a rule of thumb, if your code does not contain 'new' then there is
no need for 'delete' either (and this is a good thing). Any dynamically
allocated memory is maintained and released by the std::vector objects
internally.

That's a good rule to remember. So it is just a plain pointer. Thanks!
std::vector automatically stores the stuff in the right place, you do not
need to worry about this. Just avoid raw arrays and 'new' (as you have
done so far) and you should be fine.

Yes, haven't been using new/delete in years since I discovered smart
pointers and vectors.
 
S

Stefan Ram

A said:
There are 2 vectors each one having a structure
std::vector<MyStruct> v1;
std::vector<MyStruct> v2;

IIRC, the default constructor constructs an /empty/ vector.
This means that /neither/ vector so far has a structure.
 
A

A

Stefan Ram said:
IIRC, the default constructor constructs an /empty/ vector.
This means that /neither/ vector so far has a structure.

Of course, I know that - that was just a dumb example. Thanks for pointing
this out though. Much appreciate the effort.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top