in reply to C-like Pointers

my $bytes= 4*1024; # Set to number of bytes to copy my $copy= unpack "P$bytes", 0x78000000;

- tye        

Replies are listed 'Best First'.
Re^2: C-like Pointers (unpack)
by DerHartmut (Novice) on Nov 16, 2009 at 05:04 UTC
    Hi,

    with your solution I'm gettig a segfault while executing. This either can mean it works and the adress 0x78000000 is just wrong for it (I need to find out where Perl does run in the virtual memspace...) or your solution is wrong.

    Anyway, thanks for your advice :D

      Although less probable, seen in pack documentation:

      If your system has a strange pointer size (i.e. a pointer is neither as big as an int nor as big as a long), it may not be possible to pack or unpack pointers in big- or little-endian byte order. Attempting to do so will result in a fatal error.
Re^2: C-like Pointers (unpack)
by tye (Sage) on Nov 16, 2009 at 17:39 UTC

    Oops, I forgot that this requires an extra layer of indirection. I suspect that the number of environments where "I" is the wrong choice is very small. Strangely, I used "L" in Acme::ESP which seems a more fragile choice.

    my $fmt= "I"; my $iBytes= length( pack $fmt, $fmt ); my $pBytes= length( pack "P", $fmt ); if( $iBytes != $pBytes ) { die "Your pointers are $pBytes bytes, ints are $iBytes"; } my $pointer= pack $fmt, 0x78000000; my $bytes= 4*1024; # Set to number of bytes to copy my $copy= unpack "P$bytes", $pointer;

    - tye