Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: char table ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 8 Topic 45784 of 48044
Post > Topic >>

Re: char table / pointer memory allocation

by James Kanze <james.kanze@[EMAIL PROTECTED] > May 12, 2008 at 03:06 AM

On 12 mai, 08:58, Paavo Helde <nob...@[EMAIL PROTECTED]
> wrote:
> James Kanze <james.ka...@[EMAIL PROTECTED]
> wrote in news:9b653ea1-eadb-464e-
> abc7-11c9ee293...@[EMAIL PROTECTED]
> > On May 7, 9:47 pm, Paavo Helde <nob...@[EMAIL PROTECTED]
> wrote:
> >> meh...@[EMAIL PROTECTED]
 wrote in news:b1ee3325-348a-41ec-88bd-d3aea0a2c784
> >> @[EMAIL PROTECTED]
> >> Besides, I recommend to get familiar with std::string and
> >> forget the string lifetime issues forever.

> > I can't let that pass.  The only times I use C style strings is
> > when lifetime is an issue; a static char[] with a constant
> > initializer is static initialized, and effectively has an
> > infinite lifetime.

> And not much of use in a multithreaded application
> (presumably, char[] array is used for changing the content,
> otherwise one should just use string literals).

A string literal is a char[].  (OK, a char const[].)  But the
char[] will often occur in structures.  A char const* is more
usual, but there are exceptions.  (And of course, the char
const* points to a char const[], usually a string literal.)

> Any global mutable object will create problems in
> multithreaded environment (and if you are writing a library
> you don't know if it is going to be used in singlethreaded or
> multithreaded fa****on).

That depends on the library, and the targetted users.  I know
that the ones I work on professionally will be used in a
multithreaded environment, since they create threads themselves.

> A static char[] has to be externally locked by each access.
> This should better be encapsulated in a class managing this
> string - and voila, we are back to the statics initialization
> order problem. One needs singleton pattern or something
> similar here. And in a singleton one could easily use
> std::string as well.

> > A static std::string can easily be accessed before it is
> > constructed, or after it is destructed.

> Yes, avoid namespace-level statics, these are evil ;-) (no pun
> intended!).

Local statics aren't necessarily any better.  In fact, they can
be worse, since they aren't necessarily constructed before main.

In the end, the surest bet is statically initialized constant
values.  They're guaranteed to be initialized before any code
actually runs.  Thus, for example, I'll often use a statically
initialized struct Something const [] and std::find_if, rather
than std::map, because it is 100% free of order of
initialization issues.

> Note also that a static char[] array is of limited size.

Yes, but the compiler will define the size in accordance with
the initializer.

> The size should be checked explicitly by every mutating
> operation, which does not sound very reliable. I would argue
> this solution has problems even in single- threaded code.

Nobody suggested using char[] for mutable objects.

--
James Kanze (GABI Software)             email:james.kanze@[EMAIL PROTECTED]
 en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
 




 8 Posts in Topic:
char table / pointer memory allocation
mehafi@[EMAIL PROTECTED]   2008-05-06 11:59:53 
Re: char table / pointer memory allocation
"sk_usenet" <  2008-05-06 12:23:33 
Re: char table / pointer memory allocation
mehafi@[EMAIL PROTECTED]   2008-05-06 21:19:18 
Re: char table / pointer memory allocation
Paavo Helde <nobody@[E  2008-05-07 14:47:31 
Re: char table / pointer memory allocation
James Kanze <james.kan  2008-05-09 02:37:17 
Re: char table / pointer memory allocation
Paavo Helde <nobody@[E  2008-05-12 01:58:33 
Re: char table / pointer memory allocation
James Kanze <james.kan  2008-05-12 03:06:08 
Re: char table / pointer memory allocation
Paavo Helde <nobody@[E  2008-05-12 17:14:33 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Oct 15 22:42:45 CDT 2008.