1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

static const char* and Position Independent Code

Discussion in 'Embedded' started by Alex Vinokur, Sep 12, 2006.

  1. Alex Vinokur

    Alex Vinokur Guest

    Is this library safe?

    Does this have to do with Position Independent Code?


    ====== Library ======


    --- File foo.h ---


    struct Foo
    {
    static const char* s_name;
    // Stuff
    };


    -------------------

    --- File foo.cpp ---


    const char* Foo::s_name = "ABCD";


    // Stuff


    -------------------


    ====================


    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Sep 12, 2006
    #1
    1. Advertisements

  2. Alex Vinokur

    Tim Wescott Guest

    That depends on what you want to be safe from. I always get the syntax
    confused, but you are either declaring a constant pointer to a string of
    variable characters, or you are declaring a variable pointer to a string
    of constant characters. In either case if you want this to be safe from
    change (like you want the library to be reentrant) it isn't -- for that
    you need to declare it

    const char * const s_name;

    If you _do_ declare it with both 'const' modifiers in there the effect
    of this code should be that there will be a bit of memory with
    "ABCD\x00" stored in it, and an unchangeable pointer pointing to that
    memory. In an embedded system with a halfway decent compiler it'll be
    read-only memory. Unless someone plays games with the compiler the
    memory and pointer will never change, and unless the hardware is weird
    it can be read an unlimited number of times.

    --

    Tim Wescott
    Wescott Design Services
    http://www.wescottdesign.com

    Posting from Google? See http://cfaj.freeshell.org/google/

    "Applied Control Theory for Embedded Systems" came out in April.
    See details at http://www.wescottdesign.com/actfes/actfes.html
     
    Tim Wescott, Sep 12, 2006
    #2
    1. Advertisements

  3. Safe ... from what?
    Impossible to tell, since you didn't let us know what "this"
    actually is.
     
    Hans-Bernhard Broeker, Sep 12, 2006
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.