473,883 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C Style Strings

Hi,

I've always used std::string but I'm having to use a 3rd party library
that returns const char*s. Given:

char* pString1 = "Blah ";
const char* pString2 = "Blah Blah";

How do I append the contents of pString2 to pString? (giving "Blah
Blah Blah")

I looked at strcat but that crashes with an unhandled exception.

Thanks
Jun 1 '06
89 5191
On 4 Jun 2006 02:48:22 -0700, I waved a wand and this message magically
appeared from persenaama:
If you want this even faster, and the architecture can support it,
copy the largest size per loop, i.e. 32 bits at a time. Or even 64
bits.


Problem with that is that both source and destination have to be
aligned (for correctness, performance or both depending on the
architechture). Strings are commonly short so the overhead of
detecting this situation might degrade the performance on average use
case.


Somewhere I have a routine written in assembler that copies strings
very quickly. It's intelligent enough to know about these special cases.
--
http://www.munted.org.uk

Take a nap, it saves lives.
Jun 4 '06 #61
Alex Buell wrote:
On 4 Jun 2006 02:48:22 -0700, I waved a wand and this message magically
appeared from persenaama:

If you want this even faster, and the architecture can support it,
copy the largest size per loop, i.e. 32 bits at a time. Or even 64
bits.


Problem with that is that both source and destination have to be
aligned (for correctness, performance or both depending on the
architechture ). Strings are commonly short so the overhead of
detecting this situation might degrade the performance on average use
case.

Somewhere I have a routine written in assembler that copies strings
very quickly. It's intelligent enough to know about these special cases.


For a good example, have a look at

http://cvs.opensolaris.org/source/xr...en/memcpy.s#27

--
Ian Collins.
Jun 4 '06 #62
> Somewhere I have a routine written in assembler that copies strings
very quickly. It's intelligent enough to know about these special cases.


Can we take a look at the code and do somekind of estimate where the
tie-breaker case is concerning the string length?

Jun 4 '06 #63
> For a good example, have a look at
http://cvs.opensolaris.org/source/xr...en/memcpy.s#27


Basicly it does count/4 "rep movsd" followed by count%4 "rep movsb" for
the leftover bytes, this is not very useful for asciiz strings as the
trailing zero must be detected and correctly handled. The code does not
align the source or destination address either.

The case which was being discussed was somekind of optimized strcat()
variant, using asciiz style strings as input.

This might be of help, for dealing multiple chars at a time:

void example(unsigne d int* p)
{
unsigned int v = 0;
for ( ; !v; )
{
unsigned int u = *p++;
v = (u - 0x01010101) & ~u & 0x80808080;
}
// ...
}

Assuming the sizeof(unsigned int) == 4 on a particular platform and
that char is 8 bits, the above snip of code would scan four characters
at a time for trailing zero. The code can be easily modified to handle
strcat(), strcpy(), strlen() .. what is missing is handling alignment,
which is trivial this is just demonstrating a concept.

Jun 4 '06 #64
On 4 Jun 2006 07:21:33 -0700, I waved a wand and this message magically
appeared from persenaama:
Somewhere I have a routine written in assembler that copies strings
very quickly. It's intelligent enough to know about these special
cases.


Can we take a look at the code and do somekind of estimate where the
tie-breaker case is concerning the string length?


Seems I was incorrect, the routine is strlen() which actually counts 4
bytes at a time and returns the total asciiz string length.
--
http://www.munted.org.uk

Take a nap, it saves lives.
Jun 4 '06 #65
Noah Roberts wrote:
we******@gmail. com wrote:
/* In C: */

int catHi (bstring b) {
static struct tagbstring hi = bsStatic ("hi"); /* init-time */
bconcat (b, &hi); /* Essentially an addition and a memcpy */
}

// in C++:

void catHi (CBString &b) {
b += "hi"; // Requires implicit construction of CBString("hi") or
some
// sort of strlen() being called on "hi" before
appending.
}
The only difference between the two code slices is syntax; they mean
the same thing.


They have the same end effect -- however they are not the same in
implementation.
[...] Of course, to be truely certain of that we would have
to know what bsStatic() does,
Its a macro that uses sizeof() and "" inline concatenation to deduce
the length of the string and enforce the fact that it is a string
literal. It forms a special kind of bstring compatible structure using
pure stack or static memory. The resources are managed implicitely by
the state of the call stack.
[...] what bconcat does,
It concatenates to a dynamic bstring (which, in this case, we assume is
passed in).
[...] what CBString(const
char*)
Constructs a CBString with constant equivalent to the const char *
initializer.
[...] does and what CBString::opera tor += does.
Its like bconcat for CBStrings.
[...] What I think they all
mean just looking at the above would result in exactly the same
operations.
Conceptually, they are, but by implementation they are not. In
particular there is no way to trick the C++ part into constructing the
contents of the string form purely stack or static memory unless you
actually use the C syntax.
[...] Of course there is nothing requiring that CBString be
implemented so that += does not accept a const char* directly.
Right, but it doesn't help, because you still need to call an extra
strlen(). (Or iterate character by character but you don't get to
leverage the compiler's highly optimized memcpy().)
Even at that.... your code example does not prove your point at all.
It does not illustrate any difference between C and C++ with regard to
stack variables.


Ok -- using syntax and concepts from C++ that are not just directly
lifted from C, how does one make a static CBString such as I have
shown? (You can drop the static decorator in the front, to make this
all just about the stack, and not static memory.)

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jun 4 '06 #66

we******@gmail. com wrote:

Even at that.... your code example does not prove your point at all.
It does not illustrate any difference between C and C++ with regard to
stack variables.


Ok -- using syntax and concepts from C++ that are not just directly
lifted from C, how does one make a static CBString such as I have
shown?


Ok, I see where the difference is in the two code segments. You are
mistaking the stack for static variables apparently. Nothing keeps you
from doing this:

void f(CBString & str)
{
static CBString hi = "hi";

str += hi;
}

Besides that I fail to understand what exactly it is you think is
different. I have no idea what your functions do beyond your poor
attempt to describe them so there is no way to tell what you are
talking about. Simply put, you are completely mistaken about there
being no way to put a variable of any kind on the "stack" in C++. I
don't know how else to put it if you don't understand that. I still
can't figure out why you think otherwise.

Jun 4 '06 #67
"persenaama " <ju***@liimatta .org> writes:
Somewhere I have a routine written in assembler that copies strings
very quickly. It's intelligent enough to know about these special cases.


Can we take a look at the code and do somekind of estimate where the
tie-breaker case is concerning the string length?


The text starting with "Somewhere I have" was written by Alex Buell.

persenaama, the lines that look like

"persenaama " <ju***@liimatta .org> writes:

are called attribution lines. Don't delete them *unless* you've
trimmed everything that person wrote from your followup. They make it
significantly easier to follow the discussion.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 4 '06 #68

we******@gmail. com wrote:
Ok -- using syntax and concepts from C++ that are not just directly
lifted from C, how does one make a static CBString such as I have
shown? (You can drop the static decorator in the front, to make this
all just about the stack, and not static memory.)


void foo()
{
static const char * x = "hi";
}

void bar()
{
const char * y = "hi";
}

The difference between foo() and bar() is that the pointer object y is
in the stack while the pointer object x isn't. The string literal isn't
in the stack in either case.

However, in C++, you cannot really say that as there is no concept of
stack in C++ it is just commonly understood that a stack is a very
practical and common implementation of local scoped objects with auto
storage within a function.

I think I have to re-read what this was really about as I seem to miss
the point what stack has to do with the string literal. :)

Jun 4 '06 #69
Alex Buell wrote:
On 4 Jun 2006 07:21:33 -0700, I waved a wand and this message magically
appeared from persenaama:

Somewhere I have a routine written in assembler that copies strings
very quickly. It's intelligent enough to know about these special
cases.


Can we take a look at the code and do somekind of estimate where the
tie-breaker case is concerning the string length?

Seems I was incorrect, the routine is strlen() which actually counts 4
bytes at a time and returns the total asciiz string length.


Something like:

http://cvs.opensolaris.org/source/xr...en/strlen.s#27

?

--
Ian Collins.
Jun 4 '06 #70

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

Similar topics

6
8326
by: Christopher Benson-Manica | last post by:
I have a C-style string (null-terminated) that consists of items in one of the following formats: 14 characters 5 characters space 8 characters 6 characters colon 8 characters 5 characters colon 8 characters Items are delimited by semicolons or commas. I have to produce a string delimited only by semicolons and containing items in the first two formats only. For example,
5
2431
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My problem is in comparing the customer number I just read in with those in the list. I'm not sure how to go about comparing c-style strings for greater than, less than.. here is how I am currently trying to do it: while( ( custinfo.number >...
3
2128
by: joealey2003 | last post by:
Hi all... I included a css file on my html and i need to check some properties. The html is: <style id="myid" src="mycsspage.css"> </style> Now i need something to access it like: alert(document.getElementById("myid").bottomline.color);
7
1953
by: seans | last post by:
Hi, I need to change the font, font size, and color of a button on a form. I need to save the current style settings and restore them later. Is there an easy way of doing that? Sorry if there is a simple solution to this; I am still learning Javascript. Thanks again.
5
10233
by: Chuck Bowling | last post by:
Maybe I'm doing something wrong or just don't understand the concept, but i'm having a problem with default properties. My impression of how a default property should act is this; MyClass c = new MyClass(); c = "my text"; In the line above, the string is assigned to a field in the class instance.
2
1419
by: Xiaoshen Li | last post by:
Dear All, I hear a lot that cstring cannot be changed. But my code: /************************************************************* * Testing cstring(a pointer) * **********************************************************/ #include <iostream> using namespace std;
4
2017
by: Kza | last post by:
Hi, just in the process of maintaining some software that used some funy old string library and char*s , and we are updating everything to use std::strings. (or should I say std::basic_string<>s) I find it wierd that that all the new c++ ansi style librarys like the streams and file handling classes still expect us to use old style char* type strings. For example, ofstreams open function expects the filename as a char* parameter rather...
2
2232
by: Kamjah Kogumah | last post by:
A Dev Opera article suggest using code 2 instead of code 1 in performance-critical functions. code 1: a += 'x' + 'y'; code 2: a += 'x'; a += 'y';
16
3682
by: yu_kuo | last post by:
Is there any comparison data on perfomance difference between std::string and c style string? Or maybe if there are source code which could be used to measuer on different compiler/platform, in a systematic way?
0
9944
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11154
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7977
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6005
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3241
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.