473,387 Members | 1,687 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.

string-class

I've developed a string-class that holds the string in an array which
is a member-variable of the class and that has maximum-size which is con-
figurable through a template-parameter. If any operation would grow the
string beyond its maximum size, an exeception would be thrown. This kind
of string obviously has superior performance over a std::string because
there's never any additional memory-allocation. But before I'm going to
re-invent the wheel, I'd like to ask if someone here alreay encountered
a fully-fledged implementation of such a string-class on the web.
Jul 19 '05 #1
10 4802
> You allocate the maximum chunk of memory for *every* string object,
even for a null string? What an idea! I bet no one ever tried such
a thing.


Millions of developers tried this because it's exactly the
C-way except that I'm doing a bounds-check here.
Jul 19 '05 #2


"Oliver S." wrote:
You allocate the maximum chunk of memory for *every* string object,
even for a null string? What an idea! I bet no one ever tried such
a thing.
Millions of developers tried this


Might be. They tried this because
* it is a good exercise
* they need to practice dynamic memory management
because it's exactly the
C-way except that I'm doing a bounds-check here.


If you want it the C way, then do it the C way. In C++
this is not a good idea. And btw: you expect a *huge*
speed increase, do you? I tell you a secret: it will
not happen. The guys writing std::string aren't that
dumb. They don't reallocate every time a string gets
larger. There are good and very effective strategies to
avoid this.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3
On 28 Jul 2003 23:11:41 GMT, "Oliver S." <Fo*******@gmx.net> wrote:
I've developed a string-class that holds the string in an array which
is a member-variable of the class and that has maximum-size which is con-
figurable through a template-parameter. If any operation would grow the
string beyond its maximum size, an exeception would be thrown. This kind
of string obviously has superior performance over a std::string because
there's never any additional memory-allocation. But before I'm going to
re-invent the wheel, I'd like to ask if someone here alreay encountered
a fully-fledged implementation of such a string-class on the web.


Why not just use a fixed-size char array, and not make out-of-bounds
errors?

</dib>
John Dibling
Witty banter omitted for your protection
Jul 19 '05 #4
> If you want it the C way, then do it the C way.

In most cases you can estimate the maximum string-length a string will
have. So my suggested approach is suitable for all this cases. With the
lib I started you simply give the maximum lentgh as a template-parame-
ter and the calls would throw an exception if the bounds would be excee-
ded (a solution that does a fallback to heap-based strings in this case
would be most elegant, although I doubt that this is really needed).
In C++ this is not a good idea.
That's your opinion that bases on your personal preferences; nothing
more. If you don't need the performance, you can stick with std::strings
but otherwise the approach I suggested is very handy compared to a simple
char[].
And btw: you expect a *huge* speed increase, do you?
Yes, because there's no need for at least the single allocation/deallo-
cation-pair the std::string-approach takes. This call is either very slow
(compared to the fixed-buffer-approach) or does a lot of memory-fragmen-
tation.
I tell you a secret: it will not happen. The guys writing
std::string aren't that dumb.
That's nothing to do with the fact that even they can't avoid some
general allocation-overhead.
They don't reallocate every time a string gets larger.
Of course not; but the fixed-buffer-approach is *much* faster than a
single allocation on construction and a deallocation on destruction of
the string anyway.
There are good and very effective strategies to avoid this.


Of course; and they're trivial as well (assuming that' you're not
writing the allocator yourself). But even this strategies take per-
formance.
Jul 19 '05 #5
> Why obviously?
You underestimate the intelligence of the guys who hoave written
your standard library.
No, I don't.
In practice you seldom will find much differences in execution speed
between your implementation and the one already provided to you.


Of course you can generally find a difference; please test your std::
string-implementation before you issue such false unfounded statements.
Jul 19 '05 #6
> Why not just use a fixed-size char array, and not make out-of-bounds
errors?


Because that's by far not that handy.
Jul 19 '05 #7
On 29 Jul 2003 16:06:37 GMT, "Oliver S." <Fo*******@gmx.net> wrote:
Why not just use a fixed-size char array, and not make out-of-bounds
errors?


Because that's by far not that handy.


In that case, why not use a reserve()'d string? Or a vector<char>?

What is not handy? Writing solid code? (Not a jab) Or are there
additional methods in your class (like formatting methods) that add
handiness to the char buffer? I agree that writing solid code isn't
easy; but that's why we get paid pretty well, and that's the glamorous
life of a C/C++ programmer.

It seems to me that you are essentially using a fixed-size char[] with
extra stuff plunked on top. That extra stuff can only be useful while
still developing code, in helping find the code that causes buffer
overruns etc. That is, once you have found and eliminated the bugs in
the code that uses the strings, the extra code youv'e added to your
char[] buffer has lost its usefullness. At that point, it is less
efficient than just using the raw buffer (in terms of memory, speed
and maintainability), so why do it?

Maybe I'm missing something. You have said that in most cases the
programmer will "know" how big the buffer will need to be in advance.
But suppose they don't. Suppose they want to use your string class in
a function that takes a variable number of paramaters, like in a
specialized version of sprintf. Then you throw an exception when the
buffer gets too big, essentially telling the calling code, "You
screwed up in calling me, now deal with this." Pretty rude,
especially if the fault isn't on the caller, but the fact that a
fixed-length buffer is of very limited usefullness. Furthermore, in
order to write robust, industrial code, everybody who uses your string
class will need to wrap all thier calls in try{}catch{} blocks,
greatly degrading performance. What a hassle.

Again I ask you, why not just use a fixed-length char buffer and avoid
all this mess?

</dib>
John Dibling
Witty banter omitted for your protection
Jul 19 '05 #8
On 29 Jul 2003 21:56:36 GMT, "Oliver S." <Fo*******@gmx.net> wrote:
In that case, why not use a reserve()'d string?
Because even this needs an allocation/deallocation-pair.


Even a raw buffer needs to be allocated and deallocated. A
reserve()'s string can be reserve()'s by a ctor call, ensuring that
there aren't uneeded allocations. So this point is a wash.
A class like the one I suggested should have methods for string-typical
operations like the formatting-operations you mentioned.
[snip]
Most of the usefulness comes from the formatting-options (which are easy
to implement somewhat faster than formatting with the stupid stream-modi-
fiers of the iostream-lib). The bounds-checking is just one tiny feature.
Ok, now I think we are getting to the root of the issue. Suffer my
predictions of your reasoning, and please correct me as needed.

You feel that std::string is inadequate for 2 main reasons: 1) you
don't like the alloc/dealloc scheme it employs, and 2) it doesn't
provide comprehensive string support functions, like sprintf().
I didn't suggest the depicted string-class for general purpose ! But
I claim that a lot of std::string-uses could be replaced by such string
-objects and thereby significantly lowering the CPU-time string-opera-
tions take.


Much of industrial code consists of formatting short strings. I agree
that std::string's is often too inefficient as compared to using a raw
buffer for such formatting. But very often these formatted strings
need to then be saved in memory for a while, and a raw buffer isn't
the right tool for that job either. But combining the use of raw
buffers for formatting with std::strings for the occasional retention
of string data provides what I think is a very effective solution to
both problems. Maybe Iv'e just gotten used to this model of
programming, but I also find it to be an elegant and completely
general-purpose solution.

</dib>
John Dibling
Witty banter omitted for your protection
Jul 19 '05 #9
> Even a raw buffer needs to be allocated and deallocated.

No, there's no *extra* allocation; either it is inline with its enclo-
sing object or its on the stack.
You feel that std::string is inadequate for 2 main reasons: 1) you
don't like the alloc/dealloc scheme it employs, and 2) it doesn't
provide comprehensive string support functions, like sprintf().
For me, only the performance is relevant; if I'd develop a fully-fledged
string-class for public use, there'd be much functionality in the class I
need.
Much of industrial code consists of formatting short strings. I agree
that std::string's is often too inefficient as compared to using a raw
buffer for such formatting. But very often these formatted strings
need to then be saved in memory for a while, and a raw buffer isn't
the right tool for that job either.


Of course not, but in most cases the string could be inline with an en-
closing object so there'd no extra-allocation. Pure std::strings with no
additional context are rather rare. And of course the over-allocation has
to be balanced against the more compact "just-fits-allocation" (although
memory-fragmentation and allocation-granularity are relevant here also).
Jul 19 '05 #10


"Oliver S." wrote:
If you want it the C way, then do it the C way.


In most cases you can estimate the maximum string-length a string will
have. So my suggested approach is suitable for all this cases. With the
lib I started you simply give the maximum lentgh as a template-parame-
ter and the calls would throw an exception if the bounds would be excee-
ded (a solution that does a fallback to heap-based strings in this case
would be most elegant, although I doubt that this is really needed).
In C++ this is not a good idea.


That's your opinion that bases on your personal preferences; nothing
more.


Oh. I already tested std::string compared to fixed length character
buffers. I found that in my applications it doesn't make a difference
in terms of speed. But the safety and ease that std::string brings
is more then worth that small spees penalty. And yes: The penalty
has always been small in real world programs.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #11

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

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
2
by: s | last post by:
I'm getting compile errors on the following code: <code> #include <iostream> #include <fstream> #include <list> #include <string> using namespace std;
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
5
by: Alfonso Morra | last post by:
Hi, What is the recomended way of returning an STL container (e.g. std::string, std::vector etc fom a function? Is it by simply returning a local variable? (I doubt it) std::string...
8
by: Grant Wagner | last post by:
I'm a bit confused by String() (typeof 'string') vs new String() (typeof 'object'). When you need to access a method or property of a -String-, what type is JavaScript expecting (or rather, what...
2
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
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
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?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.