473,883 Members | 1,656 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 5189

persenaama wrote:

[snip Expression Templates ]

Sounds cool. I hadnt thought of using expression templates for this
task but it fits rather well. I am not sure the implementation would be
totally trivial, but certainly worth investigating.
Note that there is one downside IIRC, that you either need an
assignment or a conversion to prompt the evaluation, which can be
inconvenient in use. There is also the issue of tracking the data which
may causes problems, or maybe not.

Overall though... cool idea!

regards
Andy Little

Jun 4 '06 #51

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. Of course, to be truely certain of that we would have
to know what bsStatic() does, what bconcat does, what CBString(const
char*) does and what CBString::opera tor += does. What I think they all
mean just looking at the above would result in exactly the same
operations. Of course there is nothing requiring that CBString be
implemented so that += does not accept a const char* directly.

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.

Jun 4 '06 #52
"Ian Collins" <ia******@hotma il.com> wrote

Ultimately you do have to go to assembly to squeeze every last bit of
efficiency out of the code.
You shouldn't assume that library functions are always optimally
written -
memcpy is often a simple byte copying loop.


Profile, don't speculate. Many CPUs have block copy/move instructions
that the library might use.

Profile, don't speculate.

That's wonderful advice, as long as your code is always running on the same
platform. My programs have to run on anything with reasonable efficiency.

--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
Jun 4 '06 #53
Malcolm wrote:
"Ian Collins" <ia******@hotma il.com> wrote
Ultimately you do have to go to assembly to squeeze every last bit of
efficiency out of the code.
You shouldn't assume that library functions are always optimally
written -
memcpy is often a simple byte copying loop.


Profile, don't speculate. Many CPUs have block copy/move instructions
that the library might use.

Profile, don't speculate.


That's wonderful advice, as long as your code is always running on the same
platform. My programs have to run on anything with reasonable efficiency.

Then you can't use assembly and have to code to the lowest common
denominator.

When I've faced this problem, I included optimisations for known targets
or types (32/16/8 bit) of target and a fall back byte copying loop.

The specific optimisations did result from thorough profiling on those
targets.

--
Ian Collins.
Jun 4 '06 #54
Ian Collins wrote:
Malcolm wrote:

Profile, don't speculate.
That's wonderful advice, as long as your code is always running on the
same
platform. My programs have to run on anything with reasonable efficiency.

Then you can't use assembly and have to code to the lowest common
denominator.


Profiling isn't just about determining which low-level bits to split.

Profiling is also about detecting where to use brute-force, and where to
work hard to install a good high-level algorithm.

For most of your code, brute-force will not cause a bottleneck under high
load.

This is why "premature optimization is the root of all evil". Brute-force is
most likely the easiest code to read, unit test, and maintain. The
high-level algorithms cost more in programming time, so only put them in
when profiling reveals they will have a benefit.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 4 '06 #55
Phlip wrote:
Ian Collins wrote:

Malcolm wrote:


Profile, don't speculate.
That's wonderful advice, as long as your code is always running on the
same
platform. My programs have to run on anything with reasonable efficiency.


Then you can't use assembly and have to code to the lowest common
denominator .

Profiling isn't just about determining which low-level bits to split.

Who said it was?

--
Ian Collins.
Jun 4 '06 #56
Ian Collins wrote:
Profiling isn't just about determining which low-level bits to split.

Who said it was?


Take a cleansing breath, Ian. Sometimes when a post contains a quote, the
next line doesn't contradict but reinforces it. I'm aware this is the
exception with USENET, but it happens.

In this case, the mark you replied to took the position "optimizati on is
about opcodes, so profiling won't work for portability".

Optimize opcodes only as a last resort. You knew that too, but forgot to
bring the point up.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 4 '06 #57
Phlip wrote:
Ian Collins wrote:

Profiling isn't just about determining which low-level bits to split.


Who said it was?

Take a cleansing breath, Ian. Sometimes when a post contains a quote, the
next line doesn't contradict but reinforces it. I'm aware this is the
exception with USENET, but it happens.

In this case, the mark you replied to took the position "optimizati on is
about opcodes, so profiling won't work for portability".

Optimize opcodes only as a last resort. You knew that too, but forgot to
bring the point up.

Ah :)

--
Ian Collins.
Jun 4 '06 #58
> belong in clc, yet you keep it up. The only possible excuses for this
are (a) you are inexcusably stupid or (b) you are inexcusably trying to
start flame wars. Neither is acceptable. Please go away.


You are wrong, is it possible? Yes. Bye. *plonk*

Jun 4 '06 #59
> 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.

Jun 4 '06 #60

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
2127
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
9781
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
11123
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
10734
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...
1
10836
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9567
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...
0
5794
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
4606
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
4211
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3230
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.