Thread: return function pointer getting crushed??

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    79

    return function pointer getting crushed??

    I have a function that produces an array result. I return a pointer to that result. I am finding that the pointer / array looks as expected at a breakpoint right after the function returns but a few lines down the code the array values are getting crushed. I am learning that the stack gets reclaimed and yes this can and will happen. My question is what can I do right after the function returns to claim the array in main and not have it change on me? I would like to not modify the function (ie leave the pointer to the returned array) if possible.

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    Declare the array in the caller and pass a pointer to the array as an argument when calling the function. It is also wise to have a parameter for the size of the array.

    Alternatively, you can use dynamic memory allocation in the function, then the caller has the responsibility to call free when done.

    Yet another possibility is to declare the array static within the function, but that has its own pitfalls.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,709
    This NEVER works, can never work.
    Code:
    int *foo ( ) {
      int array[10];
      // do stuff
      return array;
    }
    Fix 1
    Code:
    int *foo ( ) {
      static int array[10];
      // do stuff
      return array;
    }
    The array persists past the end of the function call, but the function is not re-entrant.

    It isn't thread safe (at all), and you have to be careful to make use of the result before calling the function again.
    Code:
    int *a = foo();
    int *b = foo();  // you just trashed a

    Fix 2
    Code:
    int *foo (int *array, int size) {
      // do stuff
      return array;
    }
    This is the only way to go really.
    It gives the caller the greatest flexibility to decide what to use to store the data, whether it be a local array or some malloc'ed pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2020
    Posts
    79
    I think I solved it....I just declared the return variable as static within the function and now the results are retained....Is there any reason why this method can't be used?

  5. #5
    Registered User
    Join Date
    Mar 2020
    Posts
    79
    Laserlight: What are the pitfalls of using static as you alluded to this in your post?

    forget it as I just saw Salem's comments on this....

    I do believe it is safe as I immediately act on the results and use them before the function gets called again....This is all occurring on an embedded processor with a well defined function and purpose....

    Thank you both for your replies
    Last edited by ridgerunnersjw; 01-17-2021 at 12:29 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,709
    > I do believe it is safe as I immediately act on the results and use them before the function gets called again
    Well....

    > This is all occurring on an embedded processor with a well defined function and purpose....
    You'd have to be running with ALL interrupts disabled, but that's one hell of an assumption.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    784
    Quote Originally Posted by Salem View Post
    > This is all occurring on an embedded processor with a well defined function and purpose....
    You'd have to be running with ALL interrupts disabled, but that's one hell of an assumption.
    Only if the function is also called from an interrupt handler. If the function isn't used by any interrupt handler (only by the main code), there's no need to disable interrupts.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,709
    Oh but there is.

    Just having an interrupt happen will trash the contents of the very stack currently balanced on a knife edge of calamity.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    784
    Quote Originally Posted by Salem View Post
    Just having an interrupt happen will trash the contents of the very stack currently balanced on a knife edge of calamity.
    Well yes of course, but OP already decided to use "static" which avoids that problem altogether.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return pointer from function
    By skyr6546 in forum C Programming
    Replies: 5
    Last Post: 11-21-2019, 11:40 AM
  2. Return pointer to a vector of objects as a function return
    By Adaptron in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2016, 09:23 AM
  3. return pointer address from a function
    By chris_oliver in forum C Programming
    Replies: 9
    Last Post: 04-18-2015, 03:45 AM
  4. Replies: 23
    Last Post: 11-23-2011, 12:29 PM
  5. Can a function return a pointer to itself?
    By King Mir in forum C Programming
    Replies: 4
    Last Post: 04-19-2006, 01:15 PM

Tags for this Thread