#!/usr/bin/env perl
use warnings;
use 5.012;
use Inline 'C';
my $value = test();
say $value;
__END__
__C__
void test() {
int a = 4294967294;
Inline_Stack_Vars;
Inline_Stack_Reset;
Inline_Stack_Push( sv_2mortal( newSViv( a ) ) );
Inline_Stack_Done;
}
Output:
-2
Why do I get here an output of "-2"?
An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.
In an inline function, a function call is replaced by the actual program code. Most of the Inline functions are used for small computations. They are not suitable for large computing. An inline function is similar to a normal function.
Example 1. C++ Copy. // inline_keyword1.cpp // compile with: /c inline int max( int a , int b ) { if( a > b ) return a; return b; } A class's member functions can be declared inline, either by using the inline keyword or by placing the function definition within the class definition.
Explanation: Inline function is those which are expanded at each call during the execution of the program to reduce the cost of jumping during execution.
int a
uses probably 32-bit representation. You should use unsigned int
if you want to represent values above 4,294,967,296/2.
Perl supports both signed and unsigned integers, and its operators will switch nicely between them, but you are explicitly requesting an IV (signed int type SV). Use newSVuv instead. You also need to say UV a =
or unsigned a =
instead of int if ints are 32 bits but perl is using 64 bit integers, since otherwise the cast to UV done by newSVuv will end up extending the sign bit through the upper 32 bits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With