Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Inline::C demonstration for C programmers

by missingthepoint (Friar)
on Sep 01, 2008 at 07:05 UTC ( [id://708121] : note . print w/replies, xml ) Need Help??


in reply to Re: Inline::C demonstration for C programmers
in thread Inline::C demonstration for C programmers

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";'
  • Comment on Re^2: Inline::C demonstration for C programmers

Replies are listed 'Best First'.
Re^3: Inline::C demonstration for C programmers
by ikegami (Patriarch) on Sep 01, 2008 at 23:14 UTC

    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.

      If the DLL is unloaded, then doesn't the pointer become invalid?

      Yep, at least on my OS. In abstract terms, the virtual memory mapping for that DLL is removed when it's unloaded, and any pointers to memory in the range corresponding to that DLL's mapping are now junk.

      That still wouldn't be a problem here...

      I'll take your word for it, since I have to. My perl internals knowledge is pretty much 0. :) However, if I've actually misused Inline::C I'd be happy for someone to point it out.


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