question

DangDKhanh-2637 avatar image
1 Vote"
DangDKhanh-2637 asked DangDKhanh-2637 commented

Fill vector data question?

Hi,
In .Net I usually want to write functions like this:
array1D myarr= functioncustom(...); //return array1D of integers
with define function as this:

array1D functioncustom(..){
array1D temp;

//fill array

return temp;
}

Is there any problem if I keep this routine in c++?
I mean is the array recorded twice?

Thanks you!



c++
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Your title talks about a vector. Your text talks about an array. They are not the same. What do you mean by "recorded"?

A function cannot return an array. It can return a vector.

If you really need an array instead of a vector, you could imbed the array in a struct which the function could also return.

With either vector or struct, two objects will be created: myarr will be created at the point it is defined and exist for the life of that code block. temp will be created when you enter functioncustom and be destroyed when that function returns.

1 Vote 1 ·

Hi,
I mean the elements are copied twice,
first time when i fill data to temp in functioncustom
the second time when I assign myarr=function... (will it assign by memory address or create another copy object?)
maybe i should write a function like functioncustom(myarray1D& ) but i usually do the first way.
do they differ and affect performance?

0 Votes 0 ·

See also the aspects related to mandatory, optional and forbidden “Copy Elision”, “Return Value Optimisation” and “Named Return Value Optimisation”: https://en.cppreference.com/w/cpp/language/copy_elision.

In order to verify the optimisation, maybe check how many times the destructor and other special functions of array1D are called or print some messages, in Debug and Release configurations.

I think that the expression ‘array1D r = functioncustom(…)’ will probably build the result directly into r in case of Release.


1 Vote 1 ·

Hi,
Thanks you. this information is very helpful to me.

0 Votes 0 ·

1 Answer

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered DangDKhanh-2637 commented

A container class supporting move semantics will move the encapsulated data, not copy it.

For example -

     #include <iostream>
     #include <vector>
     using namespace std;
    
     vector<int> Func()
     {
         vector<int> tempVec{ 1, 2, 3, 4, 5 };
         cout << "Encapsulated data is at " << tempVec.data() << endl;
         return tempVec;
     }
    
     int main()
     {
         vector<int> retVec = Func();
    
         cout << "Returned vector data is at " << retVec.data() << endl;
        
         return 0;
     }


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
Thanks a lot, this makes sense to me.

0 Votes 0 ·