http://www.perlmonks.org?node_id=353262

Anyone done this before? I only wanted to mask some bits...
$number &= 00x99200308;
  • Comment on How to burn 100 megabytes in one line and still get the wrong answer
  • Download Code

Replies are listed 'Best First'.
Re: How to burn 100 megabytes in one line and still get the wrong answer
by Zaxo (Archbishop) on May 14, 2004 at 03:38 UTC

    Hah, good one!

    By doubling the leading 0, you converted this to 0 x 99200308 ; that is, a string of nearly 100,000,000 zeros. 0x99200308 will be better behaved :-)

    After Compline,
    Zaxo

Re: How to burn 100 megabytes in one line and still get the wrong answer
by Abigail-II (Bishop) on May 14, 2004 at 08:09 UTC
    Neat.
    0x0x0x0 eq 00x00 # Must come in handy some time.

    Abigail

      Urk. How to (try to) burn 4 gigabytes:
      $number &= 0x0xFFFFFFFF;
      Candidate for Most Lethal Typo award. Note: untested

      Update: OK so I tested it and it doesn't compile. Award rescinded.

        Even if it did compile, it isn't that lethal. In fact, even if you type:
        $_ = 0x0x0xFFFFFFFF
        perl will try to allocate a large piece of memory, and unless you have the memory for it, the malloc will fail, causing Perl to quickly segfault. And even if you do have the memory, at worst you have a temporary slowdown of your system. Big deal. Far more lethal are mistypings in open:
        open my $fh => ">" => "/etc/passwd" or die; # Oops, mistyped '<'
        (assuming you're running this as root).

        Abigail

Re: How to burn 100 megabytes in one line and still get the wrong answer
by tachyon (Chancellor) on May 14, 2004 at 03:47 UTC

    What is with the 00x ? Perl will be finding the x operator and giving you a string of '0' x BIG_NUM. Does this help?

    my $num = 0xffff; my $mask = 0xd0d0; printf " %b (0x%x) %d & %b (0x%x) %d ", $num,$num,$num,$mask,$mask,$mask; print '-' x 31; $num &= $mask; printf " %b (0x%x) %d", $num, $num, $num; __DATA__ 1111111111111111 (0xffff) 65535 & 1101000011010000 (0xd0d0) 53456 ------------------------------- 1101000011010000 (0xd0d0) 53456

    cheers

    tachyon

      What is with the 00x ?

      Exactly.

Re: How to burn 100 megabytes in one line and still get the wrong answer
by flyingmoose (Priest) on May 17, 2004 at 16:43 UTC

    C:\WINNT>perl -e "print 00x888888888;" Out of memory!

    Amazing, it returns really quickly. Anyone care to speculate why this dies in a split second? Is the memory management engine really that smart? I didn't think Perl had any self-imposed ulimits or anything like that, and the Windows/Linux behavior is identical. You'd think it would slurp all available memory, but it looks like it doesn't even get close to trying.

      I belive perl will see the 'x' operator and try to allocate the necessary memory in one block. The OS will quickly not find a memory range large enough and fail the request, prompting perl to die. The alternative that would cause perl to die slowly would be if 'x' allocated a small string, copied it's first parameter into it until it ran out of space, then allocated a slightly larger sting, copied the old sting in, de-allocate the old sting, then continued to copy the first parameter until it ran out of room again. But that would be horribly inefficient. (even for perl =)