Shop OBEX P1 Docs P2 Docs Learn Events
Best way to return a string from an object — Parallax Forums

Best way to return a string from an object

Chip CoxChip Cox Posts: 73
edited 2010-04-05 18:19 in Propeller 1
What is the best way ( reliability·considered better than speed )·to return or pass back a string value from an object?· I don't care if it's through storing it in an address that is passed into the routine, or whethere it's returned like a traditional inline function.·

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2010-04-03 20:17
    Returning a pointer to a string is fast, reliable and common programming practice.· However, you have to ensure that the string is stored in a "permanent" place during the time it is used.· It would not be a good idea to return a pointer to a string stored in a method's local array.· This memory would be re-used after the method returns.

    It is also common practice for the caller to pass a pointer to a buffer that the caller provides.· You have to ensure that the buffer is large enough to hold the string plus the terminating null character.· It is a good idea to pass a buffer size also, so the called routine·can check to see if the buffer is large enough for the string.
  • Chip CoxChip Cox Posts: 73
    edited 2010-04-03 20:47
    I was afraid you were going to say that. I'm trying the second option and it's working in my test program flawlessly. When I put it in the larger real program, it doesn't return anything. But, I can tell it thinks it's sending a valid value back. The way it's coded right now, if it didn't have a valid value, the routine would not return, it would just keep reading till it got one.

    I've tried debugging with viewport, but I keep running into problems every time I call an module in an object. It seems to get lost, so I can't trace through and see what's happening over there. I've spent 8 hours of a beautiful saturday on this. I think I'm going to go outside and approach it with a clear head this evening. Thanks
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-04-03 21:05
    I would suggest posting your code, and somebody will probably figure out the problem pretty quickly.· Two big pitfalls that I have encountered are stack overflows and VAR sections in multiple instances of objects.· Each instance of an object will have its own copy of variables defined in a VAR section.

    If you are using multiple cogs use very large stacks initially.· You can reduce them later on when everything is working OK.

    Post Edited (Dave Hein) : 4/3/2010 11:20:57 PM GMT
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-04-04 00:15
    There's nothing wrong with returning a pointer into the object's own VAR space when a string is returned, so long as the user knows that it has no permanency. It's a simple matter to copy a string from a method call or just to print it, obviating any need for it to hang around forever.

    You can even create multiple buffers that the called method can cycle through. This provides a little more persistence when required for, say, single expressions that include multiple calls to the object.

    -Phil
  • Chip CoxChip Cox Posts: 73
    edited 2010-04-04 15:18
    The problem I have is that my object when called from a simple program works fine. But when I call it from the real program, whatever it's returning is getting lost somewhere. And at this point, I'm only dealing with 1 cog. This is one of those points where a hobby is becoming to much like work <grin>.
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-04-04 16:37
    In these cases you should give us the sources, otherwise we can not really help!
  • Chip CoxChip Cox Posts: 73
    edited 2010-04-05 01:10
    yall answered my question. I'm not trying to do something the hard way when there is an easier way. I'm doing it right, something is just stepping on it. I've narrowed it down to about 20 lines of code in two elseif statements that are causing it. Now I just have to wait till I can get some time to figure out why code that is run prior to calling my function is impacting the return value of my function.
    thanks
    Chip
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-04-05 18:19
    Another common pitfall is using a variable before it is initialized.· This catches me every so often.· I'm always amazed how a complex system with over a 100 thousand lines of code can work, and then I get stuck on a silly error in just a few lines of code that takes me hours to figure out.
Sign In or Register to comment.