How to get the memory address of a Perl variable in XS

Discussion in 'Perl Misc' started by cyl, Nov 3, 2008.

  1. cyl

    cyl Guest

    in Perl script,

    my @arr = (1,2,3);
    xs_test(\@arr);

    in XS code

    void xs_test()
    {
    SV * sv = ST(0);

    // get the memory address of this array reference and save it,
    but how?
    }

    How do I get the memory address of \@arr in xs_test? Will the content
    in that address change when exiting the xs_test function? Thanks.
     
    cyl, Nov 3, 2008
    #1
    1. Advertising

  2. cyl <> writes:

    > in Perl script,
    >
    > my @arr = (1,2,3);
    > xs_test(\@arr);
    >
    > in XS code
    >
    > void xs_test()
    > {
    > SV * sv = ST(0);
    >
    > // get the memory address of this array reference and save it,
    > but how?


    What do you mean? That /is/ what you're doing.

    > }
    >
    > How do I get the memory address of \@arr in xs_test?


    void* address = (void*) sv;

    > Will the content in that address change when exiting the xs_test
    > function? Thanks.


    That's always possible, but you probably should at least increment the
    refcount on sv if you're planning on keeping it around.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
     
    Joost Diepenmaat, Nov 3, 2008
    #2
    1. Advertising

  3. cyl

    sisyphus Guest

    On Nov 3, 11:24 am, cyl <> wrote:
    ..
    ..
    > How do I get the memory address of \@arr in xs_test?


    Does the following return the appropriate value ?

    int xs_test(SV * arref) {
    return (int) arref;
    }

    Cheers,
    Rob
     
    sisyphus, Nov 4, 2008
    #3
  4. cyl

    cyl Guest

    On 11$B7n(B3$BF|(B, $B2<8a(B8$B;~(B36$BJ,(B, Joost Diepenmaat <> wrote:
    > cyl <> writes:
    >
    > > How do I get the memory address of \@arr in xs_test?

    >
    > void* address = (void*) sv;
    >
    > > Will the content in that address change when exiting the xs_test
    > > function? Thanks.

    >
    > That's always possible, but you probably should at least increment the
    > refcount on sv if you're planning on keeping it around.
    >
    > --
    > Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/


    I use newSVsv to clone a copy of that SV and it works now. I think the
    problem is because the SV I want to save is on the stack and is
    collected when exiting my function call.
     
    cyl, Nov 7, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Casey
    Replies:
    10
    Views:
    728
    Dan Cernat
    Oct 20, 2004
  2. John Kotuby
    Replies:
    9
    Views:
    428
    John Kotuby
    Jul 14, 2008
  3. Ognjen Bezanov
    Replies:
    1
    Views:
    1,436
    Carl Banks
    Nov 10, 2009
  4. Ognjen Bezanov
    Replies:
    1
    Views:
    632
    Carl Banks
    Nov 10, 2009
  5. Replies:
    9
    Views:
    119
    Quarco
    Jul 20, 2004
Loading...

Share This Page