Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: C Style Strings (http://www.velocityreviews.com/forums/t442953-re-c-style-strings.html)

kwikius 06-01-2006 08:15 AM

Re: C Style Strings
 
scroopy wrote:
> 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")


#include <malloc.h>
#include <cstring>

char* concat(const char * str1, const char* str2)
{
char * result = (char*) malloc(strlen( str1) + strlen (str2) + 1);
if( result != NULL){
strcpy(result,str1);
strcat(result, str2);
}
return result;
}

#include <iostream>
#include <string>

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


int main()
{
// C-style
char* str = concat(pString1,pString2);
if(str != NULL){
std::cout << str <<'\n';
free(str);
}

// C++ style
std::string str1=std::string(pString1) + pString2;
std::cout << str1 <<'\n';
}

I'm not sure if that is the optimal C method. Its interesting to note
how much better the C++ version is though!

regards
Andy Little


Richard Bos 06-01-2006 08:29 AM

Re: C Style Strings
 
"kwikius" <andy@servocomm.freeserve.co.uk> wrote:

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

>
> #include <malloc.h>
> #include <cstring>


This is not C...

> char* concat(const char * str1, const char* str2)
> {
> char * result = (char*) malloc(strlen( str1) + strlen (str2) + 1);


....and this is the wrong way to do this in C. And, really, also in C++:
use new.

So don't cross-post stuff like that. Follow-ups set.

Richard

Richard Heathfield 06-01-2006 08:44 AM

Re: C Style Strings
 
Richard Bos said:

> "kwikius" <andy@servocomm.freeserve.co.uk> wrote:
>
>> scroopy wrote:
>> > How do I append the contents of pString2 to pString? (giving "Blah
>> > Blah Blah")

>>
>> #include <malloc.h>
>> #include <cstring>

>
> This is not C...


....and it's not C++ either, so I'm not sure why you set followups to clc++.

The entire article, in fact (his, not yours), was a classic example of the
kind of thing you get in the Hackitt and Scarper school of programming.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

kwikius 06-01-2006 08:57 AM

Re: C Style Strings
 
Richard Heathfield wrote:
> Richard Bos said:
>
> > "kwikius" wrote:
> >
> >> scroopy wrote:
> >> > How do I append the contents of pString2 to pString? (giving "Blah
> >> > Blah Blah")
> >>
> >> #include <malloc.h>
> >> #include <cstring>

> >
> > This is not C...

>
> ...and it's not C++ either, so I'm not sure why you set followups to clc++.
>
> The entire article, in fact (his, not yours), was a classic example of the
> kind of thing you get in the Hackitt and Scarper school of programming.


Thats great! Thanks! Its always good to get positive feedback!

regards
Andy Little


Roland Pibinger 06-01-2006 09:32 AM

Re: C Style Strings
 
On 1 Jun 2006 01:57:05 -0700, "kwikius"
<andy@servocomm.freeserve.co.uk> wrote:
>Richard Heathfield wrote:
>> The entire article, in fact (his, not yours), was a classic example of the
>> kind of thing you get in the Hackitt and Scarper school of programming.

>
>Thats great! Thanks! Its always good to get positive feedback!


BTW, an interesting C-string library can be found here:
http://synesis.com.au/software/cstring/index.html

Best wishes,
Roland Pibinger

kwikius 06-01-2006 09:41 AM

Re: C Style Strings
 
Roland Pibinger wrote:
> On 1 Jun 2006 01:57:05 -0700, "kwikius"
> <andy@servocomm.freeserve.co.uk> wrote:
> >Richard Heathfield wrote:
> >> The entire article, in fact (his, not yours), was a classic example of the
> >> kind of thing you get in the Hackitt and Scarper school of programming.

> >
> >Thats great! Thanks! Its always good to get positive feedback!

>
> BTW, an interesting C-string library can be found here:
> http://synesis.com.au/software/cstring/index.html


It looks like a C++ style string written in C! The telling part is the
create and destroy functions, which would of course be replaced by
language facilities if written in C++.

regards
Andy Little


Martin Ambuhl 06-01-2006 07:56 PM

Re: C Style Strings
 
kwikius wrote:
> scroopy wrote:
>
>>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")

>
>
> #include <malloc.h>

This is neither a C header nor a C++ header, so off-topic in both groups
to which you posted.

> #include <cstring>

This is not a C header, so off-topic in one of the groups to which you
posted.

If you _must_ post to both <news:comp.lang.c++> and <news:comp.lang.c>,
try to make your post topical in each. As it stands, your post is
topical in neither.

There is hardly ever any excuse for posting to both newsgroups; these
are concerned with two different languages, and advice given in posts to
both is almost certainly going to be wrong, or at least non-idiomatic,
in at least one of them.

Malcolm 06-01-2006 08:31 PM

Re: C Style Strings
 
"kwikius" <andy@servocomm.freeserve.co.uk> wrote
> scroopy wrote:
>> 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")

>
> #include <malloc.h>
> #include <cstring>
>
> char* concat(const char * str1, const char* str2)
> {
> char * result = (char*) malloc(strlen( str1) + strlen (str2) + 1);
> if( result != NULL){
> strcpy(result,str1);
> strcat(result, str2);
> }
> return result;
> }
>

Perfectly unexceptional code.
It won't execute as efficiently as it might, but then most programs can
manipulate a string much faster than a human can read it, however
inefficiently written.

If we want we can do a speed-up

void fastconcat(char *out, char *str1, char *str2)
{
while(*str1)
*out++ = *str1++;
while(*str2)
*out++ = *str2++;
*out = 0;
}

this is a bit of nuisance since it throws the burden of memory allocation
onto the user, it is also rather dangerous sinvce we don't check the buffer.
But it will be very fast. That's the beauty of C, you can roll the function
to the problem you face.
>
> #include <iostream>
> #include <string>
>
> char* pString1 = "Blah ";
> const char* pString2 = "Blah Blah";
>
>
> int main()
> {
> // C-style
> char* str = concat(pString1,pString2);
> if(str != NULL){
> std::cout << str <<'\n';
> free(str);
> }
>
> // C++ style
> std::string str1=std::string(pString1) + pString2;
>

Ok what's going on here?
You have a string, and now you are calling what looks like a string
constructor to create another type of string. Why do you need two types of
string in the program? Do they behave differently when passed to cout? How
do I know that they will behave in the same way?
>
> std::cout << str1 <<'\n';
> }
>
> I'm not sure if that is the optimal C method. Its interesting to note
> how much better the C++ version is though!
>

So what's the big - O analysis of that '+' operation? Where is this
documented? What if I want to sacrifice a bit of safety for speed, as we did
with C? Can I overload the string '+' operator to achieve this?

Apologies to our friends on C++, but this was a provocative post.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm



kwikius 06-01-2006 10:04 PM

Re: C Style Strings
 
Martin Ambuhl wrote:
> kwikius wrote:


> > #include <malloc.h>

> This is neither a C header nor a C++ header, so off-topic in both groups
> to which you posted.


Ok.

> > #include <cstring>

> This is not a C header, so off-topic in one of the groups to which you
> posted.


Ok.

> If you _must_ post to both <news:comp.lang.c++> and <news:comp.lang.c>,
> try to make your post topical in each. As it stands, your post is
> topical in neither.


The code in the post attempts to highlight the different problems of
dealing with resource management in both languages. Having to deal
manually with resources and having to check results of functions for
validity are both sources of additional code complexity in C it seems.
Maybe there are plans to address this situation in the next version of
the C standard?

> There is hardly ever any excuse for posting to both newsgroups; these
> are concerned with two different languages, and advice given in posts to
> both is almost certainly going to be wrong, or at least non-idiomatic,
> in at least one of them.


If I have given incorect advice, I apologise. FWIW I certainly dont
advocate use of malloc or C-style strings or manual memory management.
IOW I advocate use of C++ over C. C holds no advantage whatever. The
concat function shows very neatly why it is best to avoid C-style
strings in C++. In C it seems that it is possible to do better though
there seems to be no standard higher level string library. Maybe there
are plans to address this situation in the next version of the C
standard?

Whatever... Happy coding!

regards
Andy Little


santosh 06-02-2006 03:35 AM

Re: C Style Strings
 
kwikius wrote:
> Martin Ambuhl wrote:
> > kwikius wrote:

.... snip ...

> The code in the post attempts to highlight the different problems of
> dealing with resource management in both languages. Having to deal
> manually with resources and having to check results of functions for
> validity are both sources of additional code complexity in C it seems.
> Maybe there are plans to address this situation in the next version of
> the C standard?


I guess you want garbage collection and exceptions support. Both impose
run-time overhead. Increasingly, C is used in embedded programming
where both might be unfeasible and unnecessary. I don't think there is
much chance that they will be standardised.

Third-party garbage collectors are available for C. But if you really
want these features built into the langauge, maybe you should consider
Java?

> If I have given incorect advice, I apologise. FWIW I certainly dont
> advocate use of malloc or C-style strings or manual memory management.


Good for you.

> IOW I advocate use of C++ over C. C holds no advantage whatever.


It depends on what you're trying to do. Sweeping generalisations aren't
correct.

> The concat function shows very neatly why it is best to avoid C-style
> strings in C++.


Indeed. If you decide to program in C++, then you should program in
C++.

> In C it seems that it is possible to do better though
> there seems to be no standard higher level string library.


Yes, anyone who wants an abstract string library has to either roll his
own or use pre-existing ones. The former case, especially, allows one
to optimise for their specific requirements, though I don't think the
code will be significantly better than std::string.

> Maybe there are plans to address this situation in the next version of the C
> standard?


I doubt it. Even the next revision of the standard is a minimum of 3-4
years away, and the standard committee have always resisted turning C
into another C++/Java wannabe.



All times are GMT. The time now is 05:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.