Shop OBEX P1 Docs P2 Docs Learn Events
Is there a short-hand for specifying size of objects pointed to (i.e. char*, float*)? — Parallax Forums

Is there a short-hand for specifying size of objects pointed to (i.e. char*, float*)?

agsags Posts: 386
edited 2013-03-26 15:46 in Propeller 1
For the purpose of only using memory when needed (as opposed to global VAR/DAT symbols), I use a method I'd describe as pseudo-stack creation. Whenever I want to return a value from a method that is larger (in size) than one long, I implement the method with a parameter that is a pointer to a location where the return value will be placed. This is not optimal, because to be reasonably safe I should also use a parameter that specifies the maximum size of that location, and have the (called) function implement checks to be sure that the maximum size is not exceeded. I presume this is a frequent issue, and I don't have a better solution (but I'm open to learning).

What I would like to know is if there is a way to avoid all the explicit size specifications when writing to the calling method's buffer. A typical pattern is that I am writing byte elements. Every time I do, I have to use the byte[baseAddress][offset] syntax (or byte[baseAddress+offset]). I find this ugly, too much typing, and error prone. Is there another pattern that can be used?

Comments

  • Mike GMike G Posts: 2,702
    edited 2013-03-26 13:12
    Sure, implement a typed object (library) that uses getters and setters; get, set(value). For an array type, use a DAT location to hold the number of objects allocated.
  • agsags Posts: 386
    edited 2013-03-26 13:22
    Not sure if I understand completely. If I do though, wouldn't that then require that I prefix every call the the getter/setter with the object name? (i.e. value := MyLib.get(address), MyLib.set(address,value))

    Also, if I want to allow use of different size objects, and arrays, I would have to reserve (in VAR/DAT) enough memory to hold the maximum number of all objects stored at any one time. I suppose I could have multiple objects. But now the issue seems to be how to dynamically allocate hum RAM? Is this possible?

    If there is an implementation that you could point me to that would be helpful.

    Thanks.
  • Mike GMike G Posts: 2,702
    edited 2013-03-26 15:05
    Also, if I want to allow use of different size objects, and arrays, I would have to reserve (in VAR/DAT) enough memory to hold the maximum number of all objects stored at any one time. I suppose I could have multiple objects. But now the issue seems to be how to dynamically allocate hum RAM? Is this possible?
    Correct, create a typed object and a collection object. Allocating enough memory to hold stuff is always a concern regardless if the logic is encapsulated or globally available in a DAT section.

    As for your original question, AFAIK, there are no other short hand commands other than what's found in the Propeller Manual.
  • agsags Posts: 386
    edited 2013-03-26 15:46
    Thanks Mike. I was hoping for some obscure way to declare a "type" (more like size) like:

    PRI CalledFunction(byte[memPtr])

    Unfortunately, I haven't been able to find any creative way to accomplish that.
Sign In or Register to comment.