Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++

Name: Anon 2009-01-28 2:07

Hey, I am a complete noob to c++, and I need to know how to add strings.

Eg, I have:
a="one"
b="two"
c="three"

and I would like "d" to equal "onetwothree".

Thank you for any help

Name: Anonymous 2009-01-29 9:51

>>39
Very funny, your point is void, thank you.

>>36
I prefer '\0', because this way, the person reading the program (me from the future) will instantly understand that this is a string terminator.

Name: Anonymous 2009-01-29 11:29

>>27
I concede that.

With gcc 4.2.1 on my machine, the common subexpressions in >>5 are actually optimized out when -O2 is used:

>>5 took 223 ms
>>11 took 927 ms
>>14 took 236 ms


Declaring the lengths as const rather than volatile allows gcc to make the same optimization, I suspect --

--- test.c.orig 2009-01-29 11:17:03.000000000 -0500
+++ test.c      2009-01-29 11:15:37.000000000 -0500
@@ -44,11 +44,10 @@
 
     then=NOW();
     for(i=0;i<HOWMANY;i++){
-        volatile unsigned int la,lb,lc;
-      
-        la=strlen(a);
-        lb=strlen(b);
-        lc=strlen(c);
+        const unsigned int
+                       la=strlen(a),
+               lb=strlen(b),
+               lc=strlen(c);
       
         d=malloc(la+lb+lc+1);

>>5 took 223 ms
>>11 took 930 ms
>>14 took 224 ms


Interestingly enough, with gcc 3.4.6, volatile produces faster code than const (though significantly slower than 4.2):

volatile:
>>5 took 1141 ms
>>11 took 1309 ms
>>14 took 746 ms

const:
>>5 took 1135 ms
>>11 took 1309 ms
>>14 took 766 ms


The signficant difference between the gcc 4.2 and 3.4 output is because the builtins for string manipulations weren't introduced until the 4.x series. This can be demonstrated by disabling builtins --

gcc34 -fno-builtin -O2 test.c
>>5 took 1446 ms
>>11 took 1434 ms
>>14 took 834 ms

gcc -fno-builtin -O2 test.c
>>5 took 1471 ms
>>11 took 1451 ms
>>14 took 896 ms


Anyway, enough of that. Let's all get back to Haskell and pretend this thread was never posted.

Name: Anonymous 2009-01-29 12:02

I wonder why the title says C++ and everyone wants you to mess with character arrays and strcpy like it was C.

Use the string class.

Name: Anonymous 2009-01-29 12:04

>>43
This was already answered in >>2.

We decided to discuss a topic which is actually meaningful, unlike stupid Sepples bullshit.

Name: Anonymous 2009-01-29 23:01

this thread needs more EXPERT ENTERPRISE JAVA6 EE

Name: Anonymous 2009-01-29 23:26

sprintf

Name: Anonymous 2009-01-29 23:34

char *d = NULL;
asprintf(&d, "%s%s%s", a, b, c);

Name: Anonymous 2009-01-30 0:30

String a = "one";
String b = "two";
String c = "three";

StringBuilder s = new StringBuilder();
s.append(a).append(b).append(c);
String d = s.toString();


I benchmarked this and it's 3% faster than >>42. Enjoy your poor performance, C fans.

Name: Anonymous 2009-01-30 0:47

>>48
Which is negated by the -5000% penalty for starting JVM

Name: Anonymous 2009-01-30 0:50

>>48
hahahaha
oh fuck you java fags. I bet you're some college fag who's never done any real programming. oooh, scared of a little assembly are we? oh no not a pointer! whatever shall we do?
goddamn java fags

Name: Anonymous 2009-01-30 0:59

>>50
hahahaha
oh fuck you c fags. I bet you're some college fag who's never done any real programming. oooh, scared of a little garbage collection are we? oh no not a factory! whatever shall we do?
goddamn c fags

Name: Anonymous 2009-01-30 1:00

>>48
try comparing it to >>31 and >>47.

also, String d = a + b + c; is actually faster than using StringBuilder in this case because the compiler is smart enough to replace a + b + c with the actual value.

Name: Anonymous 2009-01-30 1:06

>>50

Faggot, all object variables in Java are pointers. Must have had a dick in your ass instead of learning useful shit instead of legacy C shit.

>>52

Is maybe right for Java. It would be nice if it had standard byte-code mnemonics. .Net CIL does do that in that case.

Name: Anonymous 2009-01-30 1:14

Is maybe right for Java.
if you're using a compiler that doesn't suck ass, it's smart enough to do that.

Name: Anonymous 2009-01-30 1:17

>>53
if you don't understand the difference between reference and pointer semantics, than I might ask you to leave my beloved /prog/

Name: Anonymous 2009-01-31 20:59

>>55
BITCH GOT TOLD

Name: Anonymous 2011-02-03 0:19

Name: Anonymous 2011-02-04 18:54

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List