473,800 Members | 2,495 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 5172
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
8323
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
2428
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
2121
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
10230
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
1418
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
2013
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
2228
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
3676
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
9690
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
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10275
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10033
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...
1
7576
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
6811
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
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.