473,387 Members | 1,596 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,387 software developers and data experts.

Any reason for local var to be "static const"?

Looking at some code I see a declaration inside a function like

static const string s("some string");

Does the static serve any purpose here?

Sep 6 '06 #1
16 10790
On 6 Sep 2006 12:37:08 -0700 in comp.lang.c++, "Chris"
<ch***************@gmail.comwrote,
>Looking at some code I see a declaration inside a function like

static const string s("some string");

Does the static serve any purpose here?
Prevents the string from being constructed over again for every call
of the function.

Sep 6 '06 #2

Chris wrote:
Looking at some code I see a declaration inside a function like

static const string s("some string");

Does the static serve any purpose here?
Yes, static serves a purpose. Static automatic variables are created
once and aren't destroyed when the function exits. The variable s is
only constructed once. I would guess the writter of the code is trying
to improve performance by making it static.

-Brian

Sep 6 '06 #3

BigBrian wrote:
Chris wrote:
Looking at some code I see a declaration inside a function like

static const string s("some string");

Does the static serve any purpose here?

Yes, static serves a purpose. Static automatic variables are created
once and aren't destroyed when the function exits. The variable s is
only constructed once. I would guess the writter of the code is trying
to improve performance by making it static.

-Brian
If "static" weren't there and it was just declared "const", would the
compiler be likely to do the same thing--allocate the string once
somewhere and reuse it? Just wondering if the static is overkill in
practice.

Sep 7 '06 #4

Chris schreef:
BigBrian wrote:
Chris wrote:
Looking at some code I see a declaration inside a function like
>
static const string s("some string");
>
Does the static serve any purpose here?
Yes, static serves a purpose. Static automatic variables are created
once and aren't destroyed when the function exits. The variable s is
only constructed once. I would guess the writter of the code is trying
to improve performance by making it static.

-Brian

If "static" weren't there and it was just declared "const", would the
compiler be likely to do the same thing--allocate the string once
somewhere and reuse it? Just wondering if the static is overkill in
practice.
No it's not.

Static say something about scoop. (There is only one)
Const says something about value. (You can't[really shouldn't] change
it)

They are different concepts.

Sep 7 '06 #5
Colander wrote:
>
Static say something about scoop. (There is only one)
Const says something about value. (You can't[really shouldn't] change
it)

They are different concepts.
In the case under discussion (local static), your first statement is
incorrect.

static in this case says something about *LIFETIME*. The variable is
constructed exactly once and destroyed only at the end of program execution.

It has nothing to do with scope in this case, and further, you
explanation (There is only one) has nothing to do with scope either.

Sep 7 '06 #6

red floyd wrote:
Colander wrote:

Static say something about scoop. (There is only one)
Const says something about value. (You can't[really shouldn't] change
it)

They are different concepts.

In the case under discussion (local static), your first statement is
incorrect.

static in this case says something about *LIFETIME*. The variable is
constructed exactly once and destroyed only at the end of program execution.

It has nothing to do with scope in this case, and further, you
explanation (There is only one) has nothing to do with scope either.
I guess my question is--does declaring a local const string that is
initialized to a literal value as static have any real practical
purpose? I understand that static means "create once", but aren't
string literals like this created and stored somewhere "once" by the
compiler anyway?

Sep 7 '06 #7

Chris wrote:
red floyd wrote:
Colander wrote:
>
Static say something about scoop. (There is only one)
Const says something about value. (You can't[really shouldn't] change
it)
>
They are different concepts.
>
In the case under discussion (local static), your first statement is
incorrect.

static in this case says something about *LIFETIME*. The variable is
constructed exactly once and destroyed only at the end of program execution.

It has nothing to do with scope in this case, and further, you
explanation (There is only one) has nothing to do with scope either.

I guess my question is--does declaring a local const string that is
initialized to a literal value as static have any real practical
purpose?
Yes.
I understand that static means "create once", but aren't
string literals like this created and stored somewhere "once" by the
compiler anyway?
No. You are confusing a std::string with an array of const char
(a.k.a. a "C-string" or "string literal"). The latter should only be
created once (or at least typically is, whether or not that is required
by the Standard). A std::string that was _not_ static, on the other
hand, should be constructed each time the function is entered, and
destroyed each time the function is exited. Every Single Time. It is
possible that a good optimizer might optimize that construction and
destruction away, given that the std::string is decleared const, but in
practice few if any do.

In sum: I think your confusion arises from the fact that the
std::string is initialized with a string literal. That string literal
may well be created only once, but the std::string will be constructed
and destructed multiple times. And that means that putting in "static"
does have a practical purpose. Whether that purpose - saving all those
gratuitous constructions and destructions - matters here could only be
determined by profiling the code.

Best regards,

Tom

Sep 7 '06 #8

Thomas Tutone wrote:
Chris wrote:
red floyd wrote:
Colander wrote:

Static say something about scoop. (There is only one)
Const says something about value. (You can't[really shouldn't] change
it)

They are different concepts.

>
In the case under discussion (local static), your first statement is
incorrect.
>
static in this case says something about *LIFETIME*. The variable is
constructed exactly once and destroyed only at the end of program execution.
>
It has nothing to do with scope in this case, and further, you
explanation (There is only one) has nothing to do with scope either.
I guess my question is--does declaring a local const string that is
initialized to a literal value as static have any real practical
purpose?

Yes.
I understand that static means "create once", but aren't
string literals like this created and stored somewhere "once" by the
compiler anyway?

No. You are confusing a std::string with an array of const char
(a.k.a. a "C-string" or "string literal"). The latter should only be
created once (or at least typically is, whether or not that is required
by the Standard). A std::string that was _not_ static, on the other
hand, should be constructed each time the function is entered, and
destroyed each time the function is exited. Every Single Time. It is
possible that a good optimizer might optimize that construction and
destruction away, given that the std::string is decleared const, but in
practice few if any do.

In sum: I think your confusion arises from the fact that the
std::string is initialized with a string literal. That string literal
may well be created only once, but the std::string will be constructed
and destructed multiple times. And that means that putting in "static"
does have a practical purpose. Whether that purpose - saving all those
gratuitous constructions and destructions - matters here could only be
determined by profiling the code.

Best regards,

Tom
Thanks, that explains it perfectly and answers my questions.

Sep 7 '06 #9
Chris posted:
Looking at some code I see a declaration inside a function like

static const string s("some string");

Does the static serve any purpose here?

When you finish reading every else's replies, you might want to read up on
this:

http://en.wikipedia.org/wiki/Reentrant

--

Frederick Gotham
Sep 8 '06 #10

Frederick Gotham wrote:
Chris posted:
Looking at some code I see a declaration inside a function like

static const string s("some string");

Does the static serve any purpose here?


When you finish reading every else's replies, you might want to read up on
this:

http://en.wikipedia.org/wiki/Reentrant

--

Frederick Gotham
Ok, read it. What am I supposed to glean from it? Please elaborate.

Sep 11 '06 #11
Chris posted:
Ok, read it. What am I supposed to glean from it? Please elaborate.
To think twice before introducing static data into a function. Here's an
example of a non-reentrant function which will malfunction if run
concurrently by two separate threads. At first thought, it may be a good
idea to use static data, but beware...

(The aim of the function is to centre a string horizontally in a space of
specified width.)

#include <assume all necessary includes...

template<std::size_t width>
char const *CenterHoriz(char const *const p)
{
using std::memset; using std::strlen; using std::size_t;

char static spaces[width+1] = {};
memset(spaces,' ',width);

size_t const len = strlen(p);
assert(width >= len);

char *const pos = spaces + (width/2 - len/2);

memcpy(pos,p,len);

return spaces;
}

--

Frederick Gotham
Sep 11 '06 #12
Frederick Gotham wrote:
Chris posted:
>Ok, read it. What am I supposed to glean from it? Please elaborate.

To think twice before introducing static data into a function. [..]
It has nothing to do with static *const*, though, does it?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 11 '06 #13
Victor Bazarov posted:
It has nothing to do with static *const*, though, does it?

There will only be a problem if static data is altered (as in my example). If
the static data remains constant, there shouldn't be a problem.

--

Frederick Gotham
Sep 11 '06 #14
Frederick Gotham wrote:
Victor Bazarov posted:
>It has nothing to do with static *const*, though, does it?


There will only be a problem if static data is altered (as in my
example). If the static data remains constant, there shouldn't be a
problem.
So, your example is not applicable in the case discussed in this
thread, right?
Sep 11 '06 #15
Victor Bazarov posted:
>There will only be a problem if static data is altered (as in my
example). If the static data remains constant, there shouldn't be a
problem.

So, your example is not applicable in the case discussed in this
thread, right?

To be honest I didn't pay much attention to the original code snippet. I just
saw a question pertaining to static data and thought I'd throw in about
"Reentrant functions".

--

Frederick Gotham
Sep 11 '06 #16

Frederick Gotham wrote:
Victor Bazarov posted:
There will only be a problem if static data is altered (as in my
example). If the static data remains constant, there shouldn't be a
problem.
So, your example is not applicable in the case discussed in this
thread, right?


To be honest I didn't pay much attention to the original code snippet. I just
saw a question pertaining to static data and thought I'd throw in about
"Reentrant functions".
Would not a "reentrant function" by definition protect data that can be
stomped on by opposing threads? At least that is how I've always heard
it discussed. For instance, compiling a special "reentrant std lib"
protects static member data used in such functions as the C function
strtok.

I believe you are meaning that using a non-reentrant function that
manipulates static data is dangerous. Of course this is a given, as is
a non-reentrant function that manipulates global data. In fact the
problem of using non-reentrant functions in a threaded program are so
numerous that pointing out one and saying, "watch out, it could cause
problems in threaded systems," seems rather a moot point when arguing
against using such constructs.

Sep 11 '06 #17

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

Similar topics

3
by: Ajax Chelsea | last post by:
can not the "static const int" be replaced by "static enum" anywhere? is it necessary that define special initialization syntax for "static const int"?
2
by: heinquoi | last post by:
hello, i have the code: class window { static const HWND hWnd; static const HINSTANCE hInst; // ... next code here window (HINSTANCE ); }
9
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts...
6
by: .rhavin grobert | last post by:
hello;-) i frequently need the following construction: ReturnParam Function() { /...do something.../ someType var = something; /...do something.../ return something;
2
by: chenxinleo | last post by:
Hi, When i use some standard library functions and fields,which return char* type(like ctime in time.h, optarg in getopt.h)and do not have to be freed after calling,i always worry about memory...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.