473,378 Members | 1,138 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Global const strings


Let's say you have a global const variable for the name of your application.

Which do you think is preferrable?:

A) char const g_application_name[] = "ChocolateCheese";

B) const char* const g_application_name = "ChocolateCheese";
A -> The only drawback I'm thinking of is that maybe the string will be in
memory twice, and that it would be copied into my "application_name" array
from somewhere else in memory.

B -> There's no worries about it being in memory twice, but still an extra
pointer may be allocated.
Whenever I make a global string like this, I always find myself pondering
for ~20 seconds over which I should go for, so I may aswell burry it once
and for all!
Also... can anyone summarize the rules for global const variables please? I
know there's some bullshit along the lines that they can't be accessed from
within another translation unit unless "extern" is applied to the
*definition*. How exactly does that work?

-JKop
Jul 22 '05 #1
9 4590

Maybe even:

char const (&g_application_name)[sizeof("ChocolateCheese")] =
"ChocolateCheese";
-JKop
Jul 22 '05 #2

"JKop" <NU**@NULL.NULL> wrote in message
news:Ae*******************@news.indigo.ie...

Let's say you have a global const variable for the name of your application.
Which do you think is preferrable?:

A) char const g_application_name[] = "ChocolateCheese";

B) const char* const g_application_name = "ChocolateCheese";
A -> The only drawback I'm thinking of is that maybe the string will be in
memory twice, and that it would be copied into my "application_name" array
from somewhere else in memory.
No

char const g_application_name[] = "ChocolateCheese";

is an abbreviation for

char const g_application_name[] = { 'C', 'h', 'o', ...

There is only one copy of the string, so I always go for A.


B -> There's no worries about it being in memory twice, but still an extra
pointer may be allocated.
Whenever I make a global string like this, I always find myself pondering
for ~20 seconds over which I should go for, so I may aswell burry it once
and for all!
Also... can anyone summarize the rules for global const variables please? I know there's some bullshit along the lines that they can't be accessed from within another translation unit unless "extern" is applied to the
*definition*. How exactly does that work?


extern char const g_application_name[] = "ChocolateCheese";

john
Jul 22 '05 #3
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2u*************@uni-berlin.de...

"JKop" <NU**@NULL.NULL> wrote in message
news:Ae*******************@news.indigo.ie...

Let's say you have a global const variable for the name of your application.

Which do you think is preferrable?:

A) char const g_application_name[] = "ChocolateCheese";

B) const char* const g_application_name = "ChocolateCheese";
A -> The only drawback I'm thinking of is that maybe the string will be
in
memory twice, and that it would be copied into my "application_name"
array
from somewhere else in memory.


No

char const g_application_name[] = "ChocolateCheese";

is an abbreviation for

char const g_application_name[] = { 'C', 'h', 'o', ...


More precisely, {'C', 'h', 'o', ... , 'e', '\0'}

There is only one copy of the string, so I always go for A.
I don't suppose it makes much difference, but I always go for B. The
functions in C and C++ which take C strings as arguments are generally
declared as const char *, so if you use the array notation you are always
depending on an array decaying to a pointer. Of course if you use a lot of
subscripting an array may be a better description, but I usually use pointer
arithmetic. I'll bet we both use std::string for almost all of our string
processing anyway.


B -> There's no worries about it being in memory twice, but still an
extra
pointer may be allocated.
Whenever I make a global string like this, I always find myself pondering
for ~20 seconds over which I should go for, so I may aswell burry it once
and for all!
Also... can anyone summarize the rules for global const variables please? I
know there's some bullshit along the lines that they can't be accessed

from
within another translation unit unless "extern" is applied to the
*definition*. How exactly does that work?


extern char const g_application_name[] = "ChocolateCheese";


Right answer to the question or course, but it still looks like a hack to
me. I generally put my globals in an unnamed namespace and provide a
function for read access if necessary.

Yeah, I'm in the mood to split hairs tonight. doh

john

Jul 22 '05 #4
JKop wrote:
Let's say you have a global const variable for the name of your application.

Lets avoid the global topic now. :-)
Which do you think is preferrable?:

A) char const g_application_name[] = "ChocolateCheese";

This creates an array on the stack of the needed size with those
characters including the terminating 0.
B) const char* const g_application_name = "ChocolateCheese";

This points to the string literal itself. The string literal in both
cases is stored in an implementation-defined storage area, in both cases.
The second occupies less space. However in most cases,
const string g_application_name = "ChocolateCheese";
would suffice.



A -> The only drawback I'm thinking of is that maybe the string will be in
memory twice, and that it would be copied into my "application_name" array
from somewhere else in memory.

B -> There's no worries about it being in memory twice, but still an extra
pointer may be allocated.
Whenever I make a global string like this, I always find myself pondering
for ~20 seconds over which I should go for, so I may aswell burry it once
and for all!
Also... can anyone summarize the rules for global const variables please? I
know there's some *pepper* along the lines that they can't be accessed from
within another translation unit unless "extern" is applied to the
*definition*. How exactly does that work?

Definitions of globals can be accessed from other translation unit,
unless they are defined as static (inherited from C) or in an anonymous
namespace (the C++ better way).
extern sometype someobj; is a declaration in a translation unit that
tells that this global is defined (and is accessible) in another
translation unit.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #5
Ioannis Vranos wrote:
Definitions of globals can be accessed from other translation unit,
unless they are defined as static (inherited from C) or in an anonymous
namespace (the C++ better way).
extern sometype someobj; is a declaration in a translation unit that
tells that this global is defined (and is accessible) in another
translation unit.

I just realised that you had asked about const globals. Yes const
globals have local scope only (internal linkage).

Check the following from TC++PL on how you can make them with external
linkage:
"Global variables that are local to a single compilation unit are a
common source of confusion and are best avoided. To ensure consistency,
you should usually place global consts and inlines in header files only
(9.2.1).

A const can be given external linkage by an explicit declaration:
// file1.c:

extern const int a = 77;

// file2.c:

extern const int a;

void g()
{
cout << a << ´\n´;
}

Here, g() will print 77."

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #6
"Global variables that are local to a single compilation unit are a
common source of confusion and are best avoided. To ensure consistency,
you should usually place global consts and inlines in header files only
(9.2.1).

A const can be given external linkage by an explicit declaration:
// file1.c:

extern const int a = 77;

// file2.c:

extern const int a;

void g()
{
cout << a << ´\n´;
}

Here, g() will print 77."

Am I the only one that sees the blatant contradiction?

First it says "place global consts in header files only" (I'm assuming
"definition" is meant by this), then it gives an example in which the global
const is defined in a source file.
-JKop
Jul 22 '05 #7
The second occupies less space.


You're saying that
const char* const g_application_name = "ChocolateCheese";
occupies less space than:
char const g_application_name[] = "ChocolateCheese";
Can you please elaborate on that?
-JKop
Jul 22 '05 #8
JKop wrote:
Am I the only one that sees the blatant contradiction?

First it says "place global consts in header files only" (I'm assuming
"definition" is meant by this), then it gives an example in which the global
const is defined in a source file.

No, the book being complete, suggests placing them in the header file
and then provides the alternate solution.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #9
JKop wrote:
The second occupies less space.

You're saying that
const char* const g_application_name = "ChocolateCheese";
occupies less space than:
char const g_application_name[] = "ChocolateCheese";
Can you please elaborate on that?

The string literal is stored in both cases in an implementation reserved
space (that is not in the stack or in the free store), in addition to
the array.

Of course a compiler may not store it in the second case.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gianguz | last post by:
I'd like to discuss about the opportunity to have a global objects creator that introduces into a general framework (suited for multithreading) a controlled semantic to manage globals variables...
3
by: Eric Lilja | last post by:
Hello, I have a few global variables in my program. One of them holds the name of the application and it's defined in a header file globals.hpp (and the point of definition also happen to be the...
7
by: Lyn | last post by:
Hi and Season's Greetings to all. I have a question regarding the use of a qualifier word "Global". I cannot find any reference to this in Access help, nor in books or on the Internet. "Global"...
31
by: Andrej Prsa | last post by:
Hi! What happens to a globally defined pointer, e.g. void *value; that points to a particular type in a function: int dummy_func (int a) {
5
by: .NET beginner | last post by:
Hi all I need to create some strings, constants etc. in my class library that need to be shared across all the classes in my class library. How do I accomplish this? Do these strings and...
4
by: G.Esmeijer | last post by:
Hi, I would like to use a couple of hundred constant string variable throught my program. I want to use them from every corner of the software. In Vb I jst declared the global and it worked ....
6
by: D. Shane Fowlkes | last post by:
I posted this on another forum, and as I feared, the response(s) were too complex and sophisticated. I certainly don't mind learning new methods, in fact, that's why I asked, but I was hoping to...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
17
by: Andrea Taverna (Tavs) | last post by:
Subject: Initialization of a const matrix implemented as pointer-to-pointer Hello everyone. I've got the following matrix definition in a source file static const char **a; I need it to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.