vector <int*> pointer
is declared as a public member of the class A.
|
|
Iteration 1: Storing Data = 1 Storing Data = 2 Storing Data = 3 Storing Data = 4 Storing Data = 5 Iteration 2: Storing Data = 11 Storing Data = 12 Storing Data = 13 Storing Data = 14 Storing Data = 15 Iteration 3: Storing Data = 21 Storing Data = 22 Storing Data = 23 Storing Data = 24 Storing Data = 25 Iteration 1: Retrieved Data = 1 Retrieved Data = 2 Retrieved Data = 3 Retrieved Data = 4 Retrieved Data = 5 Iteration 2: Retrieved Data = 11 Retrieved Data = 12 Retrieved Data = 13 Retrieved Data = 14 Retrieved Data = 15 Iteration 3: Retrieved Data = 21 Retrieved Data = 22 Retrieved Data = 23 Retrieved Data = 24 Retrieved Data = 25 |
vector <int*> pointer
is declared as private, as below:
|
|
Iteration 1: Storing Data = 1 Storing Data = 2 Storing Data = 3 Storing Data = 4 Storing Data = 5 Iteration 2: Storing Data = 11 Storing Data = 12 Storing Data = 13 Storing Data = 14 Storing Data = 15 Iteration 3: Storing Data = 21 Storing Data = 22 Storing Data = 23 Storing Data = 24 Storing Data = 25 Iteration 1: Iteration 2: Iteration 3: |
a[i]->getPointer().push_back(x);
getPointer()
as vector <int*> &
.
|
|
Iteration 1: Storing Data = 1 Storing Data = 2 Storing Data = 3 Storing Data = 4 Storing Data = 5 Iteration 2: Storing Data = 11 Storing Data = 12 Storing Data = 13 Storing Data = 14 Storing Data = 15 Iteration 3: Storing Data = 21 Storing Data = 22 Storing Data = 23 Storing Data = 24 Storing Data = 25 Iteration 1: Retrieved Data = 1 Retrieved Data = 2 Retrieved Data = 3 Retrieved Data = 4 Retrieved Data = 5 Iteration 2: Retrieved Data = 11 Retrieved Data = 12 Retrieved Data = 13 Retrieved Data = 14 Retrieved Data = 15 Iteration 3: Retrieved Data = 21 Retrieved Data = 22 Retrieved Data = 23 Retrieved Data = 24 Retrieved Data = 25 |
|
|
new int
) is pushed (command: push_back(x)
) into pointer array after the integer value is stored into that memory location (refer line 48 to 51 of working code 1); ==16747== HEAP SUMMARY: ==16747== in use at exit: 324 bytes in 21 blocks ==16747== total heap usage: 30 allocs, 9 frees, 492 bytes allocated |
new A
and new int
but there is no delete
statement. In the real case, objects will only be created at the first part of the program, and then later those objects will be used repeatedly until the program ends. Then the operating system will clear up all allocated memory.
int
to represent another class (class B
) in the real environment. Above is just a simplified code to illustrate the problem.
|
|
L B wrote: |
---|
You should never rely on the OS to free memory. |
pointer.size()
but I store explicitly as a variable, for performance.delete
those class instances, but to let the OS clear up the memory after the program ends.Is it true that we should never rely on OS to free memory? |
Is it true that we should never rely on OS to free memory? |
activecat wrote: |
---|
The value of "quantity_of_B" needs to be retrieved quite frequently in many loops. Yes, it could be replaced by pointer.size() but I store explicitly as a variable, for performance. |
activecat wrote: |
---|
Yes A is just a wrapper of B. Just imagine class A as "Family", class B as "People". Then we create many instances of Family, each family has various number of family members (instances of People). Then of course every people (instance of People) has its own private data. |
using Bvec = std::vector<B *>;
quantity_of_B
could be replaced by pointer.size()
|
|
|
|
|
|
|
|
======= Creating families and their respective members ======= For the 1th family A Person is created, ID assigned = 1804289383 A Person is created, ID assigned = 846930886 For the 2th family A Person is created, ID assigned = 1681692777 For the 3th family A Person is created, ID assigned = 1714636915 A Person is created, ID assigned = 1957747793 A Person is created, ID assigned = 424238335 A Person is created, ID assigned = 719885386 A Person is created, ID assigned = 1649760492 ======= Retrieving families and their respective members ======= Status: 1th family has 2 family members. A Person is retrieved, ID = 1804289383 A Person is retrieved, ID = 846930886 Status: 2th family has 1 family members. A Person is retrieved, ID = 1681692777 Status: 3th family has 5 family members. A Person is retrieved, ID = 1714636915 A Person is retrieved, ID = 1957747793 A Person is retrieved, ID = 424238335 A Person is retrieved, ID = 719885386 A Person is retrieved, ID = 1649760492 |
new
statement), hence no need to clear up memory !
|
|