Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Inline::C demonstration for C programmers

by lidden (Curate)
on Sep 01, 2008 at 04:07 UTC ( #708106=note: print w/replies, xml ) Need Help??


in reply to Inline::C demonstration for C programmers

Are the strings in *inflect guaranteed to exist after you leave it? I think not.
  • Comment on Re: Inline::C demonstration for C programmers

Replies are listed 'Best First'.
Re^2: Inline::C demonstration for C programmers
by missingthepoint (Friar) on Sep 01, 2008 at 07:05 UTC

    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.

        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";'
Re^2: Inline::C demonstration for C programmers
by ikegami (Patriarch) on Sep 01, 2008 at 06:27 UTC

    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.

Re^2: Inline::C demonstration for C programmers
by syphilis (Archbishop) on Sep 01, 2008 at 06:16 UTC
    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.

    Cheers,
    Rob
      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.

      sas

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://708106]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2023-11-30 23:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?