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

C

cyl

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.
 
J

Joost Diepenmaat

cyl said:
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.
 
S

sisyphus

..
..
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
 
C

cyl

void* address = (void*) sv;


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

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top