Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > String constant

Reply
Thread Tools

String constant

 
 
James Kanze
Guest
Posts: n/a
 
      06-09-2008
On Jun 8, 12:32 pm, Frank Birbacher <(E-Mail Removed)> wrote:

> Ian Collins schrieb:


> > No particular reason, just habit. I'm just used to thinking
> > of a constant pointer to a string literal. A string literal
> > has to have an address, so where's the extra pointer?


> The pointer itself also has an address. It's one address more
> than the array solution has. The array solution saves
> sizeof(char*) bytes (or whatever) in the resulting object file
> (or it is optimized away).


One less level of indirection can often help the optimizer.

But more generally, conceptually, I think of this as giving a
symbolic name to a string literal. The same as something like:
int const toto = 43 ;
gives a symbolic name to the int const. For the most part, in
fact, coming from C, I think of such const variables as
replacing a hash define. So instead of:
#define TOTO 43
#define TITI "Hello!"
I have:
int const toto = 43 ;
char const titi[] = "Hello!" ;
With exactly the same types I had with the #define.

Fundamentally, of course, one might question the wisdom of
defining any form of string constant in a header file. Unlike
the case of integral constants, and to a lesser degree, at least
on some architectures, floating point constants, there's not
much the compiler can do with the initialization value. So
something like:
extern char const titi[] ;
seems generally more appropriate in the header. With, in some
source file:
extern char const titi[] = "Hello" ;
(And I would usually prefer char const[] for this, rather than
std::string, to be clear of any order of initialization issues.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      06-09-2008
On Jun 8, 12:35 pm, Ian Collins <(E-Mail Removed)> wrote:
> Frank Birbacher wrote:
> > Ian Collins schrieb:
> >> No particular reason, just habit. I'm just used to
> >> thinking of a constant pointer to a string literal. A
> >> string literal has to have an address, so where's the extra
> >> pointer?


> > The pointer itself also has an address. It's one address
> > more than the array solution has. The array solution saves
> > sizeof(char*) bytes (or whatever) in the resulting object
> > file (or it is optimized away).


> That was my point, it will more then likely be optimised away.


But the extra pointer reduces the probability that the string
literal itself will be optimized away, since it is actually
"used". (Of course, a good compiler should be able to follow
the link---having suppressed the pointer, the string literal is
no longer used. Usual practice for automatic variables, but I
don't know whether this is frequently done for static variables
or not.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
 
anon
Guest
Posts: n/a
 
      06-12-2008
James Kanze wrote:
> On Jun 6, 4:02 pm, anon <(E-Mail Removed)> wrote:
>> tech wrote:
>>> Hi, I need to define some strings in a header file, they are
>>> to be const Whats the best to choose from below;

>
>>> const std::string s = "Hello";
>>> const char* s = "Hello";
>>> char* s = "Hello";

>
>> I am doing one of two:
>> const std::string s1 = "Hello";
>> const char* const s2 = "Hello";

>
> Why the pointer?


Why not?


PS Sorry for very slow response
 
Reply With Quote
 
anon
Guest
Posts: n/a
 
      06-12-2008
James Kanze wrote:
> On Jun 6, 4:02 pm, anon <(E-Mail Removed)> wrote:
>> tech wrote:
>>> Hi, I need to define some strings in a header file, they are
>>> to be const Whats the best to choose from below;

>
>>> const std::string s = "Hello";
>>> const char* s = "Hello";
>>> char* s = "Hello";

>
>> I am doing one of two:
>> const std::string s1 = "Hello";
>> const char* const s2 = "Hello";

>
> Why the pointer?


I just read your other response.
Some functions/methods are expecting const char* as parameter. I know I
could do s1.c_str(), but I could pass directly const char*
I guess just a matter of preferences
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
concatenate a constant to constant string using macros sinbad C Programming 7 06-19-2008 05:28 PM
pointers to constant characters and constant pointers to characters sam_cit@yahoo.co.in C Programming 4 12-14-2006 11:10 PM
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments