I was experimenting with various older Perl interpreters, and I have noticed that a single scalar variable cannot be longer than 4095 bytes in DOS using Perl 4. You can have multiple scalar variables, each 4095 bytes, but a single one cannot be longer. If you try to make it longer, it says, "Memory allocation error" and you find yourself staring at the command prompt. (4095 = 0xfff)

In Perl 5 under DOS, the limitation seems to have been raised to 268,435,455 bytes (0xfffffff bytes). If you try to make a string that is even one byte longer, it says, "Perl is out of memory." In TinyPerl 5.8 running on Windows XP, the Perl interpreter simply crashes and Windows says, "Perl Command Line Interpreter has encountered a problem and needs to close. We are sorry for the inconvenience."

It seems that the string length was stored in a word (16-bit variable) in Perl 4, and then later this was upgraded to a 32-bit long, but in both cases the upper 4 bits are "reserved." Why are the upper 4 bits reserved? Is this pattern still holding true for more modern versions of Perl? So, I would expect that in latter versions of perl, the string length is stored in 8 bytes perhaps, and again the upper 4 bits are reserved for some reason, which means the theoretical max length of a scalar is 0xfff ffff ffff ffff which, if I'm correct, would be 1 Exabyte minus one. Am I right?

(In QBASIC 1.1 under DOS, the memory appears to be shared between the program and the variables, and I think it depends how much free space you have in the system, because I was able to create a string that is 29700 bytes, but if I tried to create larger, it said, "Out of memory." If I tried to create a second string variable after this and I placed a single byte into it, then lines of my program started disappearing, which is kind of weird. Anyway, it seems that QBASIC stores the string length in a 16-bit word. And of course, we all know that C is designed to use ASCIZ strings, so it doesn't store the string length like other languages.)


In reply to maximum length of scalar in theory by harangzsolt33

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.