This question has already been answered:
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
rez = (tube*)MyFunc();
. And the return of the array from the function is on the side. - AnT 4:19 pm