Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pointer to a vector

94 views
Skip to first unread message

A

unread,
Feb 12, 2014, 2:46:12 PM2/12/14
to
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

unread,
Feb 12, 2014, 2:51:39 PM2/12/14
to
Actually I just found that I can use *v like this:

(*v)[i].a = i + 1;

Cleaner to me than

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

However, my questions are still the same.


Paavo Helde

unread,
Feb 12, 2014, 3:11:43 PM2/12/14
to
"A" <a@a.a> wrote in news:ldgj2i$2gc$1...@gregory.bnet.hr:

> 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;
> }

You can also use references:

std::vector<MyStruct> & v = (condition)? v1 : v2;
...
v[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.

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.

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

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.

Cheers
Paavo




Jorgen Grahn

unread,
Feb 12, 2014, 3:12:49 PM2/12/14
to
On Wed, 2014-02-12, A wrote:
> 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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

A

unread,
Feb 12, 2014, 3:32:29 PM2/12/14
to
"Jorgen Grahn" <grahn...@snipabacken.se> wrote in message
news:slrnlfnle0.8...@frailea.sa.invalid...
> 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.

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

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


A

unread,
Feb 12, 2014, 3:34:45 PM2/12/14
to
"Paavo Helde" <myfir...@osa.pri.ee> wrote in message
news:XnsA2D2E1C8E2580m...@216.196.109.131...
> 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.


Message has been deleted

Jorgen Grahn

unread,
Feb 12, 2014, 4:01:57 PM2/12/14
to
On Wed, 2014-02-12, A wrote:
> "Jorgen Grahn" <grahn...@snipabacken.se> wrote in message
> news:slrnlfnle0.8...@frailea.sa.invalid...

[...]

>>> 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.
>
> A guy here was mentioning heap so I though it was related. Seems it is not:
> http://facepunch.com/showthread.php?t=780439

I don't know which of them, but several of them seem rather confused.
You'll get better answers here, with the right questions.

A

unread,
Feb 13, 2014, 8:23:50 AM2/13/14
to
"Stefan Ram" <r...@zedat.fu-berlin.de> wrote in message
news:vector-201...@ram.dialup.fu-berlin.de...
> 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.


Juha Nieminen

unread,
Feb 19, 2014, 1:34:50 PM2/19/14
to
A <a@a.a> wrote:
> a) does the above *v needs to be deleted?

Did you write the 'new' keyword anywhere? If not, then why exactly do
you think you would need to write 'delete'?

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---
0 new messages