Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Inline C"-question

Tags:

c

types

inline

perl

#!/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"?

like image 603
sid_com Avatar asked Feb 27 '11 11:02

sid_com


People also ask

What does inline in C mean?

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.

What is inline function in C with example?

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.

What is inline function give example?

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.

What is an inline function MCQS?

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.


2 Answers

int a uses probably 32-bit representation. You should use unsigned int if you want to represent values above 4,294,967,296/2.

like image 105
Benoit Avatar answered Oct 17 '22 02:10

Benoit


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.

like image 38
ysth Avatar answered Oct 17 '22 01:10

ysth