Announcement

Collapse
No announcement yet.

Allocating string space...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Allocating string space...

    I have a question:

    How do I find out how much variable space I need to allocate
    if I wish to store up to 800 strings with a maximum of 255 characters
    in each string? Do I just multiply 800 by 255 or what?

    Thanks in advance for any responses.

    ------------------
    Few cats act their age, while
    most just cough up furballs.
    Few cats act their age, while
    most just cough up furballs.

  • #2
    For dynamic strings, the space required is 4 bytes per string + one byte per character actually stored.

    For fixed-length strings, the space required is the length of the fixed string.

    If you do it with an array, add 64 (I think) bytes for an array descriptor; plus the data in the array is limited to 64K unless you use HUGE.


    Code:
    DIM HUGE X(799) AS STRING * 255  ' fixed buffer
     OR
    DIM X(799) AS STRING             ' dynamic string
    You don't need HUGE with a dynamic string array because all that counts against the 64K limit for non-HUGE arrays is the four-byte string descriptor.

    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Be also aware that fixed strings are not allocated in the same
      memory space as dynamic strings. For all dynamic strings together
      you have a single segment of max. 32 KB available, so you could not
      have 800 dynamic strings of 255 characters each (but you could have
      800 shorter strings if their total does not exceed 32 KB).
      Fixed length strings, on the other hand, can be allocated anywhere
      in memory (they are in this sense more similar to an array of BYTES
      than to a dynamic string), so the only limit is the actually available
      memory (including EMS with VIRTUAL arrays).

      Hans Ruegg

      Comment


      • #4
        Originally posted by Hans Ruegg:
        Be also aware that fixed strings are not allocated in the same memory space as dynamic strings.
        That statement depends on how you qualify the far heap. They are both stored in convention memory.

        For all dynamic strings together you have a single segment of max. 32 KB available, so you could not have 800 dynamic strings of 255 characters each (but you could have 800 shorter strings if their total does not exceed 32 KB).
        That statement is wrong, of course youi can allocate 800x255 byte strings - they only take up around 200Kb.
        Individual string segments may be up to 32760 bytes (or less depending on the $STRING metastatement). All dynamic strings (including dynamic string array subscripts) are stored in these string segments. When one segment becomes full, a new segment is allocated, and the process repeats until the far heap is exhausted. As string segments are freed, they can be reallocated to new strings again. You can use FRE(-1) to determine how much of the far heap is available for string segments, but how many strings can fit in there will depend on how many can be slotted into the string segments contained therein.

        If the app is never going to use strings longer than 255 bytes, then using a smaller $STRING segment size may utilise the far heap more efficiently. This is all described in the doc's.

        Fixed length strings, on the other hand, can be allocated anywhere in memory (they are in this sense more similar to an array of BYTES than to a dynamic string), so the only limit is the actually available memory (including EMS with VIRTUAL arrays).
        That statement could have been explained more clearly. Internally, memory for fixed-length strings is allocated at runtime and from there, their location does not vary (within their sub/function scope). However, a dynamic string will likely be moved with each allocation to the string.

        In summary: please read the doc's... they explain how memory string memory is allocated and freed, and it is essential reading for any PB/DOS programmer.

        ------------------
        Lance
        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        Lance
        mailto:[email protected]

        Comment


        • #5
          I am sorry for confusing others while being confused... I do not have the
          printed documentation, only the online help which does not contain these
          details. So I obviously misunderstood the comments on the string segment size
          and thought that there was only one string segment (also based on the fact
          that FRE(0) never returned more that 32750 bytes). Thank you, Lance, for
          taking the time to correct my errors!
          It would be interesting, in order to answer Paul's original question, to know
          about the differences in memory requirements for dynamic strings and fixed
          length strings. For example, does creating an array of fixed length strings
          also imply the creation of string handles? (Maybe this is also answered in the
          docs, but as I stated above, I do not have them.) I would guess the answer is
          no, as the location of a fixed string inside the array can be calculated based
          on the string length. If this is true, an array of fixed-length strings would
          require slightly less memory than an array of dynamic strings of the same
          length.

          Hans Ruegg

          Comment


          • #6
            Only dynamic strings have handles. So, yes, fixed-length strings use a bit less
            memory than dynamic strings of the same length.

            ------------------
            Tom Hanlin
            PowerBASIC Staff

            Comment

            Working...
            X