letsc
letsc

Reputation: 2115

Returning c++ pointers to perl

I have a function in C++ such as:

void* getField(interface* p)   
{  
  int* temp = new int( p->intValue);  
  cout<< "pointer value in c++" << temp << "value of temp = " << temp << endl;  
  return temp;  
}  

I am using SWIG to generate wrappers for the above class. Now I am trying to get the returned pointer value in Perl. How do i do it??
I have written the following perl script to call the function:

 perl  
 use module;  
 $a = module::create_new_interface();  
 $b = module::getField();  
 print $b,"\n";  
 print $$b, "\n";  

I ahve correctly defined the create_interface function since on calling the function getField() the correct intValue of the interface gets printed.

The output of the program is as follows:

_p_void=SCALAR(0x5271b0)
5970832
pointer value in c++ 0x5b1b90 value of temp 22

Why are the two values - the pointer in C++ and the reference in perl different? How do i get the intValue from the refernce? (i.e the value 22 )

Upvotes: 3

Views: 391

Answers (1)

cjm
cjm

Reputation: 62109

Because you printed one in decimal and one in hexadecimal:

 printf "pointer is 0x%x\n", $$b;   # prints "pointer is 0x5b1b90"

Perl doesn't normally use pointers. The pack and unpack functions can deal with pointers (see Packing and Unpacking C Structures), but the normal Perl idiom would be to have the function return an integer instead of a pointer to an integer.

Upvotes: 5

Related Questions