1

Topic: Help I do not understand

#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
class t {
public:
t () {
std:: cout <<"run constructor" <<std:: endl;
}
t (const t &v) {
std:: cout <<"run copy constructor" <<std:: endl;
}
int s = 1;
};
std:: vector <t *> getVector () {
std:: vector <t *> s {};
t tt {};
t tt1 {};
s.emplace_back (&tt);
s.emplace_back (&tt1);
return s;
}
int main () {
std:: vector <t *> aVector = getVector ();
for (t *tt: aVector) {
printf ("%d", tt-> s);
}
}

Dear experts;
Prompt why such code deduces 10
Though should deduce 11.

2

Re: Help I do not understand

And still a question
t tt {};
t tt1 {};
These two objects have been created on a stack
As soon as we quitted a method - to what we generally address being iterated on a vector?

3

Re: Help I do not understand

All I understood the problem reason
The question is removed
Changeover on
t *tt = new t ();
t *tt1 = new t ();
s.emplace_back (tt);
s.emplace_back (tt1);
Solved a problem

4

Re: Help I do not understand

Still a question
std:: vector <t> getVector () {
std:: vector <t> s {};
t *tt = new t ();
t *tt1 = new t ();
s.push_back (*tt);
s.push_back (*tt1); - why on this point there is a call  a class t?
return s;
}

5

Re: Help I do not understand

semen.s.semen wrote:

Still a question
std:: vector <t> getVector () {
std:: vector <t> s {};
t *tt = new t ();
t *tt1 = new t ();
s.push_back (*tt);
s.push_back (*tt1); - why on this point there is a call  a class t?
return s;
}

Because the vector extends.

6

Re: Help I do not understand

semen.s.semen wrote:

And still a question
t tt {};
t tt1 {};
These two objects have been created on a stack
As soon as we quitted a method - to what we generally address being iterated on a vector?

To garbage.
It is not necessary to use pointers once again if STL and there is no understanding.
But as a whole, it is necessary to teach a good lesson concepts of lifetime of objects and a point of their creation and removal.
Otherwise the code will be nonoptimal because of creations/copyings of piece objects. Time  in slightly more slowly.

7

Re: Help I do not understand

semen.s.semen wrote:

All I understood the problem reason
The question is removed
Changeover on
t *tt = new t ();
t *tt1 = new t ();
s.emplace_back (tt);
s.emplace_back (tt1);
Solved a problem

But added memory leak

8

Re: Help I do not understand

semen.s.semen wrote:

t *tt = new t ();
t *tt1 = new t ();
s.emplace_back (tt);
s.emplace_back (tt1);

If to do so, it is necessary to clean storage which was selected with function new after usage
Namely: to be transversed on a vector and to delete all its elements

for (t *tt: aVector) {
delete tt;
}

If it would not be desirable to delete storage manually (never better it manually to do) it is necessary everywhere t* to replace on shared_ptr <t>, and tt = new t (); to replace on tt = make_shared <t> ();

9

Re: Help I do not understand

semen.s.semen wrote:

#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
class t {
public:
t () {
std:: cout <<"run constructor" <<std:: endl;
}
t (const t &v) {
std:: cout <<"run copy constructor" <<std:: endl;
}
int s = 1;
};
std:: vector <t *> getVector () {
std:: vector <t *> s {};
t tt {};
t tt1 {};
s.emplace_back (&tt);
s.emplace_back (&tt1);
return s;
}
int main () {
std:: vector <t *> aVector = getVector ();
for (t *tt: aVector) {
printf ("%d", tt-> s);
}
}

Dear experts;
Prompt why such code deduces 10
Though should deduce 11.

Generally that the code should nothing, can deduce everything, everything.

10

Re: Help I do not understand

semen.s.semen wrote:

And still a question
t tt {};
t tt1 {};
These two objects have been created on a stack
As soon as we quitted a method - to what we generally address being iterated on a vector?

That's it that to what. To garbage.
To a shade of father Hamlet.

11

Re: Help I do not understand

semen.s.semen wrote:

Still a question
std:: vector <t> getVector () {
std:: vector <t> s {};
t *tt = new t ();
t *tt1 = new t ();
s.push_back (*tt);
s.push_back (*tt1); - why on this point there is a call  a class t?
return s;
}

Does not arise.
The code too wrong, if that.

12

Re: Help I do not understand

MasterZiv wrote:

it is passed...
Does not arise.

As  https://wandbox.org/permlink/DXb0QP66YmWi8NNp

13

Re: Help I do not understand

a guest wrote:

it is passed...
As  https://wandbox.org/permlink/DXb0QP66YmWi8NNp

There on 5 objects 3 calls .

14

Re: Help I do not understand

For that understanding about what speaks a guest, deliver

s.reserve (2);

before the first push_back.

15

Re: Help I do not understand

Dima T wrote:

it is passed...
There on 5 objects 3 calls .

Also will be all of them more and more in process of vector growth.
As correctly answered a guest happens  a vector.
Usage reserve relieves from "superfluous"

std:: vector <t> getVector () {
std:: vector <t> s {};
==> s.reserve (2);
t *tt = new t ();
t *tt1 = new t ();
s.push_back (*tt);
s.push_back (*tt1); - on this point there is no call  a class t
return s;
}

16

Re: Help I do not understand

OoCc wrote:

also will be all of them more and more in process of vector growth.
As correctly answered a guest happens  a vector.
Usage reserve relieves from "superfluous"

Or it is possible to make noexcept the move-designer. The such will be caused at  a vector if exists, in exchange to the designer of copying.

17

Re: Help I do not understand

NekZ wrote:

it is passed...
Or it is possible to make noexcept the move-designer. The such will be caused at  a vector if exists, in exchange to the designer of copying.

No. It is bad. In most cases changeover of the designer of copying by the designer of relocation of the big advantage does not give.  it is necessary to eliminate. Otherwise this  works in a-lja-dzhava a mode.

18

Re: Help I do not understand

OoCc wrote:

Is not present. It is bad. In most cases changeover of the designer of copying by the designer of relocation of the big advantage does not give .  it is necessary to eliminate. Otherwise this  works in a-lja-dzhava a mode.

? About changeover of one designer by another nobody spoke.
Move-semantics allows to avoid copying at forced . Certainly, when you know an approximate or exact amount of elements it is necessary to use reserve. But, the vector so is designed to extend, is forced moving objects to new area of storage when rests against the size of the selected unit. So, when there is a possibility to organize the move-designer and assignment statement for elements in a vector - is better to use this possibility.

19

Re: Help I do not understand

NekZ wrote:

move-semantics allows to avoid copying at forced .

It is possible more in detail. It is not strong in modern With ++.
In With ++ is not present , i.e. realloc () it is not used. Magnification of the size of a vector it at least memcpy () if thus to stop calls  and designers it is possible to spare on it time.

20

Re: Help I do not understand

It seems that the simple container with pointers is necessary to the dude. I think, the answer necessary to it will be unique_ptr:

class CSome
{
static int nInstanceCounter;
public:
CSome (const int _n): nField (_n), nInstanceNumber (++ nInstanceCounter)
{
wcout <<L "CSome ctor {nField =" <<nField <<", nInstanceNumber =" <<nInstanceNumber <<"}" <<endl;
}
CSome (const CSome& some): nField (some.nField), nInstanceNumber (++ nInstanceCounter)
{
nField = some.nField;
wcout <<L "CSome copy ctor {nField =" <<nField <<", nInstanceNumber =" <<nInstanceNumber <<"}" <<endl;
}
~CSome ()
{
wcout <<L "CSome dtor {nField =" <<nField <<", nInstanceNumber =" <<nInstanceNumber <<"}" <<endl;
}
int nField;
int nInstanceNumber;
private:
CSome () {}
};
int CSome:: nInstanceCounter = 0;
int main ()
{
vector <unique_ptr <CSome>> vs;
vs.push_back (make_unique <CSome> (5));
vs.push_back (make_unique <CSome> (6));
getchar ();
return 0;
}

21

Re: Help I do not understand

semen.s.semen, if you not in course in brackets for unique_ptr <CSome> parameters as for designer SCome i.e. if at you the designer looks CSome (5) also the pointer it is necessary to create as make_unique <CSome> (5) are specified.

22

Re: Help I do not understand

Dima T wrote:

It is possible more in detail. It is not strong in modern With ++.
In With ++ is not present , i.e. realloc () it is not used.

the call realloc' did not mean. It is simply convenient term designating certain sequence of actions in implementation std:: vector'. For example, in the dock  is used the term "Reallocation". Not an essence, in general.

Dima T wrote:

Magnification of the size of a vector it at least memcpy () if thus to stop calls  and designers it is possible to spare on it time.

memcpy looking for what. For POD-types memcpy - quite to itself the decision.
And here for complex types of classes and structures not all so is simple. At least because the object in a vector then at all will not have a possibility though somehow to notify an external world that it appears moved to the new address. Well or at least because in object pointers can be saved internal fields (why not?) that makes them  after copying in a new place. Generally,  there are no warranties that memcpy'ed the object will work , for it Implementation Defined. Therefore on the classic of old pluses, are caused copy-ctor'y on just selected piece of storage through placement new and then . It is clear that on an idea the designer of copying creates an object copy while move-ctor simply changes possession of internal resources of object. I advise to look this short video from Jayson Turner who on a bright example explains as it works.
[youtube=AG_63_edgUg]

23

Re: Help I do not understand

NekZ wrote:

it is passed...
? About changeover of one designer by another nobody spoke.
Move-semantics allows to avoid copying at forced . Certainly, when you know an approximate or exact amount of elements it is necessary to use reserve. But, the vector so is designed to extend, is forced moving objects to new area of storage when rests against the size of the selected unit. So, when there is a possibility to organize the move-designer and assignment statement for elements in a vector - is better to use this possibility.

. And move semantics it that for an animal? It also is changeover of designers of copying by designers of relocation at  a vector. We read again my previous message further.

24

Re: Help I do not understand

NekZ wrote:

But, the vector so is designed to extend, is forced moving objects to new area of storage when rests against the size of the selected unit. So, when there is a possibility to organize the move-designer and assignment statement for elements in a vector - is better to use this possibility.

Nonsenses. In 99 % cases vector changeover on  solves all problems with internal relocation and superfluous .

25

Re: Help I do not understand

OoCc wrote:

And move semantics it that for an animal?

RTFM

OoCc wrote:

It also is changeover of designers of copying by designers of relocation at  a vector. We read again my previous message further.

Yes there is no changeover. Designers of copying and relocation can coexist in one class easily. Whether the vector at  looks through type_traits there is at T noexcept a move-designer and uses it. If it is not present, it will use the copy-designer. If also it is not present, tries to find normal move-konstuktor. If it is not present, will be compiled nothing. We read at first section Exceptions / (since a C ++ 11) . We substantiate  video from my previous message Further.