Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How to get the variable or function address in memory?

by hash (Scribe)
on Nov 02, 2004 at 04:00 UTC ( #404504=perlquestion: print w/replies, xml ) Need Help??

hash has asked for the wisdom of the Perl Monks concerning the following question:

FOr example in C code:
addr_ptr = argv[1];
I get the address:
./getaddrvar HOME
HOME is stored at address 0xbffffeef
So how can i do this in perl? I`ve read something about:
$b = \$a;
But the hexadecimal`s value has only 7 digits,
on the oher hand using the C code above i got 8! Where is the problem? Thank you all for helping me.

Replies are listed 'Best First'.
Re: How to get the variable or function address in memory?
by Joost (Canon) on Nov 02, 2004 at 08:12 UTC
    There really is no reason to get at the address from perl. In fact both your examples are completely useless, but I assume that's because they're examples :-) If you want to interface with C code at this low level, you're better off using XS, or Inline::C.

    Note that a perl scalar variable is a much more complex data structure than a C string, but both XS and Inline::C can make the conversion (almost) transparant.

    updated: corrected typo

Re: How to get the variable or function address in memory?
by Mr. Muskrat (Canon) on Nov 02, 2004 at 04:41 UTC

    Generally with perl, just having the reference is enough. However, if you really think you need it then you can stringify the reference (sounds like what you have done already) to find out what type of reference it is (not the best way to do so, see ref) and where in perl's memory it resides.

      An even better way than using ref() is to use &reftype and &refaddr in Scalar::Util.

      ihb

      See perltoc if you don't know which perldoc to read!
      Read argumentation in its context!

Re: How to get the variable or function address in memory?
by zentara (Archbishop) on Nov 02, 2004 at 13:54 UTC
    I'm not a devel expert, but here is some code posted awhile back.
    #!/usr/bin/perl use Devel::Peek; my $str = 'the key is:'; my $key = 'hello'; Dump($key); decrypt( $str, $key ); $key = 'gone!'; # $key = 'x'x100000; # a string that won't fit will change PV Dump($key); decrypt( $str, $key ); sub decrypt { warn "\nGot @_\n\n" }

    I'm not really a human, but I play one on earth. flash japh
Re: How to get the variable or function address in memory?
by rcaputo (Chaplain) on Nov 09, 2004 at 19:20 UTC

    The memory address is the numeric version of the reference. Some sample code follows. As an added bonus, it illustrates the use of the mysterious "P" unpack() template.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://404504]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-08-13 20:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?