size of returning pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anuvanand1
    New Member
    • Oct 2007
    • 22

    size of returning pointer

    i have a fn of type
    short* funname (char*)

    when i call this function it returns short array
    how to find the the size of that returned array
  • DumRat
    New Member
    • Mar 2007
    • 93

    #2
    Originally posted by anuvanand1
    i have a fn of type
    short* funname (char*)

    when i call this function it returns short array
    how to find the the size of that returned array
    You can't. Return an object instead. Or pass another param by reference
    to the function to get the size. I don't know which is the better of these two methods.

    Comment

    • mac11
      Contributor
      • Apr 2007
      • 256

      #3
      Another common way of doing it (especially in C) is to supply the buffer and size of that buffer to the function and have it just fill the buffer rather than allocate it.

      Also, this way the caller is less likely to forget that he needs to deallocate the buffer.

      Comment

      • Studlyami
        Recognized Expert Contributor
        • Sep 2007
        • 464

        #4
        Creating an array in a function then returning the array is dangerous. The array will go out of scope as soon as the function ends, so there is no guarantee that the pointer will still be pointing to valid data. If you need to fill an array in the function pass the array into the function and use it.

        Comment

        • dharanidhar
          New Member
          • Apr 2008
          • 7

          #5
          can can declare that array as global variable?so that it's scope never ends with the function...

          Comment

          • Studlyami
            Recognized Expert Contributor
            • Sep 2007
            • 464

            #6
            Originally posted by dharanidhar
            can can declare that array as global variable?so that it's scope never ends with the function...
            you COULD, but shouldn't. Before weakness gets to post this link i will.

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              If you allocate memory for the array (using malloc/new/whatever) and return a pointer to it, it should remain valid in any scope. Just remember to free()/delete that pointer eventually.

              Comment

              • Savage
                Recognized Expert Top Contributor
                • Feb 2007
                • 1759

                #8
                Originally posted by Ganon11
                If you allocate memory for the array (using malloc/new/whatever) and return a pointer to it, it should remain valid in any scope. Just remember to free()/delete that pointer eventually.
                How it should?

                e.g

                [CODE=c]void *Func(void)
                {
                void *p=malloc(10);
                return p;
                }[/CODE]

                Here p is in data zone of Func,but it points to the beginning of some memory segment on the heap,when function exits data zone is deallocated and therefore
                p is no more.How are you going to find address of that memory block?

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  You're right that p doesn't exist, but your return value will carry the value of p back to wherever it was called. What is the value of p? The address of that block of memory. So the block of memory was created and still exists, and you can get the address of it by capturing the return value of your function, but p itself no longer exists (remember p was just a local variable allocated on the stack).

                  Comment

                  • Studlyami
                    Recognized Expert Contributor
                    • Sep 2007
                    • 464

                    #10
                    your return value will carry the value of p back to wherever it was called. What is the value of p? The address of that block of memory. So the block of memory was created and still exists,
                    After the return you know where the structure begins, but you don't know how many values are in it. In order to avoid that you will need to do what DumRat suggested and pass in an int by reference to get the size of the array.

                    Comment

                    Working...