Originally Posted by
Lloyd Brombach
I need main to call a function that returns a 62 byte array.
I then need that array to be passed to another function that will verify the data for use and pass a 1 back to main or reject the data and return an error of some sort so main knows to try again.
You cannot actually return an array from a function. The syntax does not exist to declare such a function, and if you attempt to return an array, what you will end up returning is a pointer to the first element of the array, which would be a Bad Thing if the array is a non-static local.
There are a few ways around this problem. One way is to declare the array in main, then pass a pointer to it to the function:
Code:
void foo(unsigned char* bytes)
{
// populate bytes[0] to bytes[61]
// ...
}
int main(void)
{
unsigned char bytes[62];
foo(bytes);
}
However, a possibly better way would be to use an array container:
Code:
#include <array>
typedef std::array<unsigned char, 62> ByteContainer;
void foo(ByteContainer& bytes)
{
// populate bytes[0] to bytes[bytes.size() - 1]
// ...
}
int main(void)
{
auto bytes = ByteContainer{};
foo(bytes);
}
Of course, another container like std::vector<unsigned char> could work too.
For verifying the data: I recommend either a boolean (true for success, false for reject), or an "error code" if you need more control over the kind of rejection. In the event that the verification is expected to succeed, i.e., a rejection means that there was an error, you could throw an exception.
Originally Posted by
Lloyd Brombach
and then return it's value and then keep coming back to somefunction() without ever having used "delete myArray" - does it create a new array (ie - occupy another memory chunk) every loop and will eventually crash, even though the name is the same? Or will it overwrite the original myArray[] and I don't have to worry about deleting?
If you did not use new or new[], then you do not need to use delete or delete[].