Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Inline::C demonstration for C programmers

by missingthepoint (Friar)
on Sep 01, 2008 at 02:39 UTC ( [id://708102] : perlmeditation . print w/replies, xml ) Need Help??

Greetings, monks.

While playing with Inline::C, I whipped up this little bit of code. I thought 'hmm, that would make a good Inline::C demo for someone who's never used the module before.'

I know it's not much more than Inline::C-Cookbook, but in the spirit of TIMTOWTDI, ...

#!/usr/bin/perl -w use strict; use Inline 'C'; explain($_) for qw( foo bar bat baz qux ); __DATA__ __C__ /* don't need to include std C headers... perl.h header (included by default with Inline) includes them */ /* define this first so explain() can see it */ char *inflect(int num) { return num == 1 ? "st" : num == 2 ? "nd" : num == 3 ? "rd" : "th"; } void explain(char *var) { static int i = 1; printf("%s is the %d%s meta-syntactic variable.\n", var, i, inflect(i) ); ++i; }

email: perl -e 'print reverse map { chr( ord($_)-1 ) } split //, "\x0bufo/hojsfufqAofc";'

Replies are listed 'Best First'.
Re: Inline::C demonstration for C programmers
by lidden (Curate) on Sep 01, 2008 at 04:07 UTC
    Are the strings in *inflect guaranteed to exist after you leave it? I think not.

      Actually, they are guaranteed to exist. According to ISO C99 TC3 (PDF) section 6.4.5 #5, string literals (e.g. "foo", the ones in inflect()) initialize arrays with static storage duration. According to 6.2.4 #3, objects with static storage duration have a lifetime which is "the entire execution of the program".

      The strings in inflect() are not only guaranteed to exist after the function's block is left, they are guaranteed to exist before the first time the function executes (5.1.2 #1).

      In other words, that code is safe. :)

      Updated: to sound a little less hostile

      email: perl -e 'print reverse map { chr( ord($_)-1 ) } split //, "\x0bufo/hojsfufqAofc";'

        I'm actually having second thoughts. The program, in this case, is the DLL created and loaded by Inline::C. If the DLL is unloaded, then doesn't the pointer become invalid?

        That still wouldn't be a problem here since the pointer to the string will be discarded before inflect returns control to Perl (having created an SV holding a copy of the string), and thus before Inline::C has a chance to unload the DLL.

      Why wouldn't they? They're not on the stack, they're part of the program's image. Memory doesn't just free itself.

      In fact, quite the opposite problem would occur had the OP used malloc or new to create the returned string: a memory leak.

      Are the strings in *inflect guaranteed to exist after you leave it?

      Those strings are simply return values. As long as inflect() returns the appropriate one (which it does), I don't think it matters where/when/if those strings exist.

        Just to beat the last bit of life out of this horse: inflect() returns a pointer to one of the strings (alluded to by the others). Note the char* type. So, if those strings were automatics, then the GP post would have been quite correct. I.e. it would return a pointer to freed stack space.