ARRAYS FUNCTIONS

Discussion in 'C++' started by coinjo, Dec 6, 2005.

  1. coinjo

    coinjo Guest

    How to return an array from a function?
     
    coinjo, Dec 6, 2005
    #1
    1. Advertising

  2. coinjo wrote:

    Don't use all caps in the subject line.

    > How to return an array from a function?


    [we should have a faq on this]

    http://tinyurl.com/7ne54


    Jonathan
     
    Jonathan Mcdougall, Dec 6, 2005
    #2
    1. Advertising

  3. "coinjo" <> writes:

    > How to return an array from a function?


    Several alternatives:

    1. Use std::vector<>

    std::vector<int> func(size_t sz)
    {
    std::vector<int> vec;
    vec.reserve(sz);
    for (size_t i = 0; i < sz; ++i) {
    vec.push_back(i);
    }
    return vec;
    }

    2. Let the caller define the array and pass it as an argument:

    void func(int* arr, size_t sz)
    {
    for (size_t i = 0; i < sz; ++i) {
    arr = i;
    }
    }

    3. Let the function allocate an array and pass it back:

    int* func(size_t sz)
    {
    int* arr = malloc(sz * sizeof *arr);
    for (size_t i = 0; i < sz; ++i) {
    arr = i;
    }
    }

    Alternative 3 is the most complex, since the caller will be
    responsible for freeing the array. This needs a careful
    analysis of ownership and lifetime issues.

    /Niklas Norrthon
     
    Niklas Norrthon, Dec 6, 2005
    #3
  4. Niklas Norrthon <> writes:

    > 3. Let the function allocate an array and pass it back:
    >
    > int* func(size_t sz)
    > {
    > int* arr = malloc(sz * sizeof *arr);
    > for (size_t i = 0; i < sz; ++i) {
    > arr = i;
    > }
    > }


    Sorry for replying to my own post, but the above is C and not C++
    of course. In C++ the value returned by malloc must be cast. But
    better is:

    int* func(size_t sz)
    {
    int* arr = new int[sz];
    for (size_t i = 0; i < sz; ++i) {
    arr = i;
    }
    }

    And the caller must free the array with delete[] sz;
    To my defence I can only say that I commonly use malloc in C
    and rarely new [], and delete [] in C++, where I usually
    choose one of the container classes instead.

    /Niklas Norrthon
     
    Niklas Norrthon, Dec 6, 2005
    #4
  5. coinjo

    BobR Guest

    Niklas Norrthon wrote in message <>...
    >Niklas Norrthon <> writes:
    >
    >> 3. Let the function allocate an array and pass it back:

    >[snip]
    >Sorry for replying to my own post, but the above is C and not C++
    >of course. In C++ the value returned by malloc must be cast. But
    >better is:
    >
    >int* func(size_t sz){
    > int* arr = new int[sz];
    > for (size_t i = 0; i < sz; ++i) { arr = i;}
    >}
    >
    >And the caller must free the array with delete[] sz;
    >To my defence I can only say that I commonly use malloc in C
    >and rarely new [], and delete [] in C++, where I usually
    >choose one of the container classes instead.
    >/Niklas Norrthon


    The way I see it, 'sz' is passed into the func 'by value'. That means a
    temporary 'sz' is created in the func and destroyed when the func returns.
    But, that is one thing, losing track of the newed memory is going to leave
    you with no way to delete it. You promised to return an 'int*', but didn't.

    int* func(size_t sz){
    int* arr = new int[sz];
    for(size_t i(0); i < sz; ++i) { arr = i; }
    return arr;
    }
    // ......
    int *MyArray( func( 10 ) );
    for(size_t i(0); i < 10; ++i){ std::cout<<MyArray<<std::endl;}
    // ......
    delete [] MyArray;

    --
    Bob R
    POVrookie
     
    BobR, Dec 7, 2005
    #5
  6. "BobR" <> writes:

    > Niklas Norrthon wrote in message <>...
    > >Niklas Norrthon <> writes:
    > >
    > >> 3. Let the function allocate an array and pass it back:

    > >[snip]
    > >Sorry for replying to my own post, but the above is C and not C++
    > >of course. In C++ the value returned by malloc must be cast. But
    > >better is:
    > >
    > >int* func(size_t sz){
    > > int* arr = new int[sz];
    > > for (size_t i = 0; i < sz; ++i) { arr = i;}
    > >}
    > >
    > >And the caller must free the array with delete[] sz;
    > >To my defence I can only say that I commonly use malloc in C
    > >and rarely new [], and delete [] in C++, where I usually
    > >choose one of the container classes instead.
    > >/Niklas Norrthon

    >
    > The way I see it, 'sz' is passed into the func 'by value'. That means a
    > temporary 'sz' is created in the func and destroyed when the func returns.


    It's the local sz is destroyed, but the function was called from somewhere,
    and the caller of the function knows what sz is, so what's the problem?

    > But, that is one thing, losing track of the newed memory is going to leave
    > you with no way to delete it. You promised to return an 'int*', but didn't.


    My mistake. I typed too quick, correcting the mistake in my previous code.
    Luckily though, the compiler would have caught this one.

    /Niklas Norrthon
     
    Niklas Norrthon, Dec 7, 2005
    #6
  7. coinjo

    BobR Guest

    Niklas Norrthon wrote in message <>...
    >"BobR" <> writes:
    >
    >> Niklas Norrthon wrote in message <>...
    >> >int* func(size_t sz){
    >> > int* arr = new int[sz];
    >> > for (size_t i = 0; i < sz; ++i) { arr = i;}
    >> >}
    >> >And the caller must free the array with delete[] sz;

    [snip]
    >> >/Niklas Norrthon

    >>
    >> The way I see it, 'sz' is passed into the func 'by value'. That means a
    >> temporary 'sz' is created in the func and destroyed when the func returns.

    >
    >It's the local sz is destroyed, but the function was called from somewhere,
    >and the caller of the function knows what sz is, so what's the problem?


    The problem was this line:
    "And the caller must free the array with delete[] sz;"

    I assume it was a 'typo', otherwise the OP might think you should do (in
    main):

    size_t ArrSz(23);
    int *MyArray( func( ArrSz ) );
    // .........
    // delete [] MyArray; // correct way
    delete [] ArrSz; // Bbzzzzztt, TROUBLE!

    I'm sure you know you don't 'delete[]' a number when you meant to delete the
    memory used by the array, but, a person just starting C++ might not.
    We 'good' on this one now? :-}
    --
    Bob R
    POVrookie
     
    BobR, Dec 7, 2005
    #7
  8. "BobR" <> writes:

    > The problem was this line:
    > "And the caller must free the array with delete[] sz;"
    >
    > I assume it was a 'typo', otherwise the OP might think you should do (in
    > main):


    Of course it was a typo, and since I know what I meant, I read it as
    what I meant.

    /Niklas Norrthon
     
    Niklas Norrthon, Dec 8, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman
    Replies:
    5
    Views:
    30,891
    Chris Smith
    Jun 17, 2004
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,659
    Victor Bazarov
    Jun 21, 2004
  3. Bill Reyn
    Replies:
    3
    Views:
    2,339
    Bob Hairgrove
    Jun 22, 2004
  4. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    588
  5. Philipp
    Replies:
    21
    Views:
    1,181
    Philipp
    Jan 20, 2009
Loading...

Share This Page