Thread: Return array from function, then pass it to another. Also - array delete question

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    3

    Return array from function, then pass it to another. Also - array delete question

    I'm having trouble getting this started.

    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.

    I can handle the if else stuff in main - it's the returning the array part I'm having trouble getting straight. I get that it should be a pointer reference and I can manage reference passing/returning with variables, but this isn't so clear to me.

    2nd question on deleting - if I declare and initialize an array - lets call it
    Code:
     someFunction() 
    myArray[62] = {1}
    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?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.

    Quote 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[].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    3
    You, my dear, are a good witch. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass an array to a function
    By sjmp in forum C Programming
    Replies: 2
    Last Post: 12-20-2012, 03:44 PM
  2. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  3. Can't pass array to function
    By yougene in forum C Programming
    Replies: 9
    Last Post: 08-19-2007, 04:09 PM
  4. how can i pass this array to my function?
    By kosodo in forum C Programming
    Replies: 4
    Last Post: 04-14-2006, 09:40 PM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM

Tags for this Thread