I am afraid that I will again ask for something obvious to everyone, which for me is not obvious: I have a function that should return an array of bytes:

#pragma pack(1) struct tube { BYTE var1; size_t var2; BYTE var3; }; #pragma pack() tube* rez; BYTE *MyFunc(){ const unint sz = 6; BYTE replaced_origin[sz]; for(int i =0;i<sz;i++) replaced_origin[i]=i; return replaced_origin; } rez = (tube*)MyFunc(); 

I know this is not the right approach. Firstly, in C ++ it is not welcome to return a pointer to an array, since its output length will be unknown and, taking risks, I am ready to do it, because. in my case, the length is unchanged. Secondly, as far as I understand, the BYTE replaced_origin[sz] string BYTE replaced_origin[sz] allocated memory for the array on the stack , which means that outside the method this pointer will become invalid ?

I know that in C ++, instead of an array, they often use vector to return from a function. But it seems to me unnecessary to create a vector for a 6-byte array, the length of which is obviously known. I also know that you can use new to allocate memory on the heap. But memory in the heap is allocated more slowly than on the stack.

In general, in one word I would like to return an array from a function by copying by value, but - I am new to C ++ - I just can’t figure out how to do this correctly

Marked as a duplicate by VTT members, 0xdb , MSDN.WhiteKnight , LFC , AnT c Mar 30 at 6:13 pm

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Are you sure you really need to return an array of 6 bytes ? Perhaps instead, a function that writes these 6 bytes to the address given to it is better suited. - avp
  • Here, the first question that arises is what kind of strange frills are: rez = (tube*)MyFunc(); . And the return of the array from the function is on the side. - AnT 4:19 pm

3 answers 3

If you know the exact size at compile time - use std::array , if you do not know - strd::vector . There are no overhead expenses in the first case, in the second - minimal ones, but there are a lot of advantages - you do not have to worry about all the problems of ownership.

Worry about how much time memory allocation takes, and how much copying takes place, then, when the profiler clearly shows that this is your gag (which I personally don’t believe in, you would be kindly allocated / released memory in some particularly fast cycle , but like this, one-time?

If you are a lover of efficiency :) - create an array outside the function , and pass a pointer to it inside. It is faster and more reliable.

And yet - I am very confused that you work with the structure as with an array of bytes. Is it really necessary? What is worse?

 tube rez; void MyFunc(tube * t) { t->var1 = 0; t->var2 = 0x04030201; t->var3 = 0x05; } MyFunc(&rez); 

Or that?

 tube rez; void MyFunc(tube & t) { t.var1 = 0; t.var2 = 0x04030201; t.var3 = 0x05; } MyFunc(rez); 
  • The simplest (if we are in C ++) is to write a constructor, the role of which, in fact, MyFunc . - andy.37
  • >> confuses that you work with the structure as with an array of bytes << - it seems to me to be quite a handy tool. Unfortunately, I use an ancient compiler, it does not yet have support for C ++ 11, and as far as I understood, std :: array appeared only with it - digital-mag
  • one
    @ digital-mag - this may be a convenient, but extremely dangerous tool, implicitly suggesting that sizeof(size_t) == 4 * sizeof(BYTE) - andy.37
  • @ andy.37, well, in principle, this danger can be circumvented through the preprocessor directives #ifdef, if you specify #pragma for different bit depths of the OS? - digital-mag
  • @ andy.37 A person does not ask how to return the structure, but how to return the array :) - Harry

If we are talking about C ++, the simplest is:

 struct tube { BYTE var1 = 0x00; size_t var2 = 0x04030201; BYTE var2 = 0x05; } tube MyFunc() { return tube(); } 

Actually, in this case, the value of the function itself is zero.

    If " it seems unnecessary to you to create a vector for a 6-byte array, the length of which is known to be known, " and you don’t want to create an array in dynamic memory, that is another option. Declare it as

     static BYTE replaced_origin[sz]; 

    and return his address to health. Such an array will exist all the time the program is running. In general, the use of static variables without obvious need is considered a bad style, but nevertheless it is a completely correct solution.