static const char* and Position Independent Code

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

formatting link
formatting link

Reply to
Alex Vinokur
Loading thread data ...

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
Reply to
Tim Wescott

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.
Reply to
Hans-Bernhard Broeker

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.