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

Where to place a constant?

I have my own string class that I'm generally verry fond of compared to
std::string. For that same reason I'm trying to improve a little on it.

Since manny objects in my project will contain strings of empty size "" I
thought it would be a good idea to share en empty string allocation between
all uninitialized strings.

However I have no idea where to place this constant? :-S
I tried this:

class CBL_String
{
public:
static char * strEmptyString = ""; // it complaines about this...

};

If I do don't initialize it in the class-spec but in a cpp file I'm not sure
that it is actually initialized before used, by some other global CBL_String
variable... right?

char *CBL_String::strEmptyString = "";
Suggestions anyone?

--
Lasse
Jul 22 '05 #1
15 2018
On Mon, 16 Feb 2004 12:23:11 +0100, "Lasse Skyum" <no spam> wrote:
Suggestions anyone?


You'd be much better off using std::string.

Jul 22 '05 #2
Suggestions anyone?


You'd be much better off using std::string.


Okay... maybe so, but I don't really se why? CBL_String helps me in manny
different ways and has never been the source of any bugs in my programs.

Does std::string have a shared empty string or maybe it uses NULL as an
empty string?
If std::string also allocated an empty string if not initialized, then I see
no reason what so ever to use it...

BTW, I've also written my own CBL_ArrayList as an equivalent to vector...
ony differenceis the ability to automatically shrink if a flag is set. For
some odd reason it also outperformes vector in my insert/remove benchmarks,
probably just a bunch of settings or something.

I'm sure all this is also in Boost (or whatever C++ extentions there might
be) but I've learned so much be writing it my self.

Anyway... I just wanted to have an initialized constant :-) Might also be
relevant for other things!

--
Lasse
Jul 22 '05 #3
Lasse Skyum <no spam> wrote:
>Suggestions anyone?
You'd be much better off using std::string.


Okay... maybe so, but I don't really se why?


Simply because std::string is 'the standard'. Using it makes your code
better re-usable for starters.
Does std::string have a shared empty string or maybe it uses NULL as an
empty string?
This would be an implementation detail.
If std::string also allocated an empty string if not initialized, then I see
no reason what so ever to use it...
A decent std::string implementation will make sure that 'std::string s;' is
_really_ cheap. I would not expect any dynamic allocation in this case.
BTW, I've also written my own CBL_ArrayList as an equivalent to vector...
This strikes me as an even worse idea than having a 'new' string class
(which might have its uses for certain encoding related reasons)
ony differenceis the ability to automatically shrink if a flag is set.
For some odd reason it also outperformes vector in my insert/remove
benchmarks, probably just a bunch of settings or something.
Would be nice to see some minimal code demonstrating these results.
It's not easy to believe unless you dropped some 'important' std::vector
characteristics.
I'm sure all this is also in Boost (or whatever C++ extentions there might
be) but I've learned so much be writing it my self.


Learning is always a good reason to re-write certain standard classes.
It is rarely a good enough reason for _using_ them, though ;-)

Andre' [who isn't particularly fond of std::string either but uses it
nevertheless]
Jul 22 '05 #4
"Lasse Skyum" <no spam> wrote:
>Suggestions anyone?
You'd be much better off using std::string.


Okay... maybe so, but I don't really se why? CBL_String helps me in
manny different ways and has never been the source of any bugs in my
programs.

Does std::string have a shared empty string or maybe it uses NULL as
an empty string?


I'd say that's implementation specific. Anyway, why do you think you
need such a shared ... ehm, nothing? I mean, an empty string has no
data to share. Its size is 0, so there is nothing to read from it, so
why would you need to share that 'nothing' between empty strings? Btw:
Do you actually have that many empty strings in real programs?
If std::string also allocated an empty string if not initialized, then
I see no reason what so ever to use it...

BTW, I've also written my own CBL_ArrayList as an equivalent to
vector... ony differenceis the ability to automatically shrink if a
flag is set.
If you're happy with your own containers and strings, it's IMHO ok to
use them. Of course, you'll limit code reusability somewhat.
For some odd reason it also outperformes vector in my
insert/remove benchmarks, probably just a bunch of settings or
something.
That again depends on the implementation you use, not only on settings.
I'm sure all this is also in Boost (or whatever C++ extentions there
might be) but I've learned so much be writing it my self.
That's of course a very good reason to write your own ones, but it is
not a reason to use them in real code :-)
Anyway... I just wanted to have an initialized constant :-) Might
also be relevant for other things!


If you want to be sure that your constant is initialized before other
static objects, you might want to use the singleton pattern.

Jul 22 '05 #5
Would be nice to see some minimal code demonstrating these results.
It's not easy to believe unless you dropped some 'important' std::vector
characteristics.


Nope, it does all the copy-constructing and inplace new's needed to make it
work right. However not using allocators, just plain ol' malloc/free.

Nevermind... that's not what wanted to talk about in the first place.

--
Lasse
Jul 22 '05 #6
If you want to be sure that your constant is initialized before other
static objects, you might want to use the singleton pattern.


Singleton... well seems to be a lot of work and fiddeling just for
initializing a constant :-(

--
Lasse
Jul 22 '05 #7
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Lasse Skyum wrote:
Would be nice to see some minimal code demonstrating these results.
It's not easy to believe unless you dropped some 'important' std::vector
characteristics.

Nope, it does all the copy-constructing and inplace new's needed to make it
work right. However not using allocators, just plain ol' malloc/free.

Nevermind... that's not what wanted to talk about in the first place.

--
Lasse

Sounds like that's the source of your performance gain right there. most
implementations use a new with a goodly bit of code in it to ensure safe
memory allocation, and if not, then gracefull notification.

Evan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFAMMtaoo/Prlj9GScRAov6AJ948pesdtbnthcOEjUpXBJIKGFVFACfRCe5
OLn3htYOKLCUgi87kIKknHs=
=ZUUQ
-----END PGP SIGNATURE-----
Jul 22 '05 #8
Lasse Skyum wrote:
I have my own string class that I'm generally verry fond of compared to
std::string. For that same reason I'm trying to improve a little on it.

Since manny objects in my project will contain strings of empty size "" I
thought it would be a good idea to share en empty string allocation between
all uninitialized strings.

However I have no idea where to place this constant? :-S
I tried this:

class CBL_String
{
public:
static char * strEmptyString = ""; // it complaines about this...

};

If I do don't initialize it in the class-spec but in a cpp file I'm not sure
that it is actually initialized before used, by some other global CBL_String
variable... right?

char *CBL_String::strEmptyString = "";
Suggestions anyone?

--
Lasse


In the C++ specification, there is no order to initializing static
variables declared in global or local translation unit scope. Thus
most people initialize these variables in the constructor. Search
the web for "Scott Meyers variable initialization".

If the order of static variable initialization is important, then
use the Singleton design pattern or move the variables inside a
function. The rule for static variable initialization in a
function is "first use".

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #9
Lasse Skyum <no spam> wrote in message
news:40***********************@dread15.news.tele.d k...
I have my own string class that I'm generally verry fond of compared to
std::string. For that same reason I'm trying to improve a little on it.

Since manny objects in my project will contain strings of empty size "" I
thought it would be a good idea to share en empty string allocation between all uninitialized strings.

However I have no idea where to place this constant? :-S
I tried this:

class CBL_String
{
public:
static char * strEmptyString = ""; // it complaines about this...

};

If I do don't initialize it in the class-spec but in a cpp file I'm not sure that it is actually initialized before used, by some other global CBL_String variable... right?

char *CBL_String::strEmptyString = "";
Suggestions anyone?

--
Lasse


This compiles, try this.

class C
{
public:
static char* sk;
};
char* C::sk = "";

Put all that in a header file.

This thread is a good example of why you should not tell people what you are
doing. You will find that a great many of the regulars are much smarter
than you and instead of answering your question they will lecture you on how
and why you are doing the wrong damn thing.


Jul 22 '05 #10
"Lasse Skyum" <no spam> wrote:
If you want to be sure that your constant is initialized before other
static objects, you might want to use the singleton pattern.


Singleton... well seems to be a lot of work and fiddeling just for
initializing a constant :-(


It can be as simple as:

class CBL_String
{
****static*const char***strEmptyString()
{
const char* const empty = "";
return empty;
}
};

Actually, even this should work:

class CBL_String
{
****static*const char***strEmptyString()
{
return "";
}
};

Jul 22 '05 #11

"Lasse Skyum" <no spam> wrote in message
news:40***********************@dread15.news.tele.d k...
I have my own string class that I'm generally verry fond of compared to
std::string. For that same reason I'm trying to improve a little on it.

Since manny objects in my project will contain strings of empty size "" I
thought it would be a good idea to share en empty string allocation between all uninitialized strings.

However I have no idea where to place this constant? :-S
I tried this:

class CBL_String
{
public:
static char * strEmptyString = ""; // it complaines about this...
This is wrong for several reasons besides initialization:
strEmptyString = new char[10]; // this will compile!!
strEmptyString[3] = '!'; // This will also compile!!!

What you want is a constant string NOT an non-constant pointer to a
non-constant string.
This is declared as:

static const char EMPTY_STRING[]; // NB CONST

};

Define in cpp file:

const char CBL_String::EMPTY_STRING[] = "";

This does not require runtime initialization.
IF you don't use it in inline methods then it is better still to make it
file static rather than class static.
If I do don't initialize it in the class-spec but in a cpp file I'm not sure that it is actually initialized before used, by some other global CBL_String variable... right?

char *CBL_String::strEmptyString = "";
None of this simple stuff requires runtime initialization it just goes in
the data section
of the object file.


Suggestions anyone?


Don't use your own string - it may be wonderful but there are millions of
people out there who know
the std::string and only one who knows CBL_String.

What is the cost to your employer of you designing, coding, testing and
documenting your string?
Just documenting it as well as the std string would cost $1000!
What is the cost of future maintainers having to read your string docs
instead of just using stuff they already know?
Jul 22 '05 #12
It can be as simple as:

class CBL_String
{
static const char * strEmptyString()
{
const char* const empty = "";
return empty;
}
};


Thanks Rolf, I'll check that one out!

--
Lasse
Jul 22 '05 #13

This compiles, try this.

class C
{
public:
static char* sk;
};
char* C::sk = "";

Put all that in a header file.
Thanks, I'll try it out... however not really understanding when exactly the
initialization is taking place.
This thread is a good example of why you should not tell people what you are doing. You will find that a great many of the regulars are much smarter
than you and instead of answering your question they will lecture you on how and why you are doing the wrong damn thing.


Yes, that tend to happen every time I post here :-(

--
Lasse

Jul 22 '05 #14
On Tue, 17 Feb 2004 10:23:01 +0100, "Lasse Skyum" <no spam> wrote:

On Mon, 16 Feb 2004 17:13:51 GMT, "kevin collins" <ke********@hotmail.com> wrote:
This compiles, try this.

class C
{
public:
static char* sk;
};
char* C::sk = "";

Put all that in a header file.
Thanks, I'll try it out...


Be sure to try it out with the header-file used in two different
compilation units of the same program.

In that case you'll get a linker error.

You can¹ avoid the linker error by adding 'const' here and there, but the
effect of that is that you specify internal linkage, i.e. multiple copies,
which is what you were trying to avoid IIRC (not that # of copies matters).
however not really understanding when exactly the
initialization is taking place.


Initialization of namespace level statics in a compilation unit is
performed before the first call of a function in that compilation unit,
including call of a constructor.

This thread is a good example of why you should not tell people
what you are doing.
On the contrary, this thread is a good example of why you should tell
people what you are doing.

The best general advice so far is to use std::string.

The best advice so far regarding how to provide a string constant is
the C++ singleton pattern (a function containing a static variable).

You will find that a great many of the regulars are much smarter
than you and instead of answering your question they will lecture you on
how and why you are doing the wrong damn thing.


Yes, that tend to happen every time I post here :-(


¹) At least I think so. But I have fever and am not thinking clearly,
and darned if I'm going to check out something you should check out.

Jul 22 '05 #15

"Alf P. Steinbach" <al***@start.no> wrote in message
news:40****************@news.individual.net...
On Tue, 17 Feb 2004 10:23:01 +0100, "Lasse Skyum" <no spam> wrote:

On Mon, 16 Feb 2004 17:13:51 GMT, "kevin collins" <ke********@hotmail.com> wrote:
This compiles, try this.

class C
{
public:
static char* sk;
};
char* C::sk = "";

Put all that in a header file.
Thanks, I'll try it out...


Be sure to try it out with the header-file used in two different
compilation units of the same program.


C::sk = "definitely not empty";

In that case you'll get a linker error.

You can¹ avoid the linker error by adding 'const' here and there, but the
effect of that is that you specify internal linkage, i.e. multiple copies,
which is what you were trying to avoid IIRC (not that # of copies matters).

however not really understanding when exactly the
initialization is taking place.
Initialization of namespace level statics in a compilation unit is
performed before the first call of a function in that compilation unit,
including call of a constructor.

This thread is a good example of why you should not tell people
what you are doing.
On the contrary, this thread is a good example of why you should tell
people what you are doing.

The best general advice so far is to use std::string.


Why?

The best advice so far regarding how to provide a string constant is
the C++ singleton pattern (a function containing a static variable).

Why?
You will find that a great many of the regulars are much smarter
than you and instead of answering your question they will lecture you on how and why you are doing the wrong damn thing.


Yes, that tend to happen every time I post here :-(


¹) At least I think so. But I have fever and am not thinking clearly,
and darned if I'm going to check out something you should check out.

Jul 22 '05 #16

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

Similar topics

25
by: pm940 | last post by:
Hello. I've been reading some past discussions on the NULL vs. zero. References are always made to systems or machienes that use values other than zero to represent the NULL pointer. Although...
38
by: Red Dragon | last post by:
I am self study C student. I got stuck in the program below on quadratic equation and will be most grateful if someone could help me to unravel the mystery. Why does the computer refuse to execute...
14
by: mortb | last post by:
I'd like to build a URL using String.Format String.Format("http://server.com/page.aspx?id={0}{1:\&optionalParameter=0;&}", id, optionalParam); The idea is that I want to exclude the...
5
by: Fernando Lopes | last post by:
Hi there. In your opinion, where is the best place to put the connection string of a web application? Web.config, a constant into the code? I'm not using a component server, so it's not an...
1
by: Frank | last post by:
Hello, I need one specific value in all the applicationspecific forms and classes of my application. What is the proper place to put it? At the moment I use a module with a public constant and...
8
by: hurry | last post by:
hi, I am writing a c program in VC++ 6. I have 2 files with 3 functions. file-1 having two functions "a" and "c" file-2 having a single function "b" with function "a" as main() , "a"...
5
by: Chad | last post by:
my input file is: test my program: #include <stdio.h> #include <stdlib.h> #define LOG "/home/miss_xtc/flood/words2.txt" #define BUFF 20
37
by: Phlip | last post by:
1230987za wrote: Kanze is a classically-trained "unit tester". In some circles "unit" is a QA concept - specifically, if a test fails, you only need to inspect one unit. So "units" are...
10
by: mirandacascade | last post by:
Question toward the bottom of this post....background information immediately below. Access 97 SQL Server 2000 Please note: although the subject line uses the word 'bloat', this post is NOT...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.