String constant

T

tech

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";

Thanks
 
A

anon

tech said:
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";

Thanks

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

James Kanze

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";

You can't really put the latter two in a header file without
getting duplicate definitions, since they aren't const. But you
ignore one of the most frequent alternatives:

char const s[] = "Hello" ;

It has the advantage of allowing static initialization, but does
mean that you'll probably end up having to construct an
std::string each time you use it, which tends to offset the
initial advantage of static initialization; but the static
initialization still has the advantage of avoiding order of
initialization problems, and the compiler can also ignore the
definition if you don't actually use the variable in a module.
 
D

Daniel Pitts

anon said:
I am doing one of two:
const std::string s1 = "Hello";
const char* const s2 = "Hello";
Right, don't forget both the const's if you use char *
You want a constant pointer to a constant value.

Depending on your typical use, I would suggest const std::string, since
string adds so much utility.
 
I

Ian Collins

tech said:
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";

This is the only version here than can go in a header.
const char* s = "Hello";

This has to be

const char* const s = "Hello";

Which you use depends on how you use them. If you wish to add to one
later (say it's a root file path), use std::string. If they are
discrete tokens, const char* const can be used. But do bear in mind a
std::string will be constructed each tome you pass one to a function
with a const std::string reference parameter.
 
J

James Kanze

This is the only version here than can go in a header.

Of the forms he proposes.
This has to be
const char* const s = "Hello";

Again: what's wrong with:

char cosnt s[] = "Hello" ;

? Why do you need the extra pointer.
Which you use depends on how you use them. If you wish to add
to one later (say it's a root file path), use std::string. If
they are discrete tokens, const char* const can be used. But
do bear in mind a std::string will be constructed each time
you pass one to a function with a const std::string reference
parameter.

On the other hand, the std::string form may suffer from order of
initialization issues.
 
F

Frank Birbacher

Hi!

Ian said:
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).

Frank
 
I

Ian Collins

Frank said:
Hi!



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.
 
J

James Kanze

Ian Collins schrieb:
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.)
 
J

James Kanze

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.
 
A

anon

James said:
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 ;)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top