Tech Support > Computer Hardware > Microprocessors > static const char* and Position Independent Code
static const char* and Position Independent Code
Posted by Alex Vinokur on September 12th, 2006


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

Posted by Tim Wescott on September 12th, 2006


Alex Vinokur wrote:

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

Posted by Hans-Bernhard Broeker on September 12th, 2006


Alex Vinokur <alexvn@users.sourceforge.net> wrote:
Safe ... from what?

Impossible to tell, since you didn't let us know what "this"
actually is.

--
Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.


Similar Posts