Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / combining strings - wierd results.

Author
Message
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 19th Feb 2009 01:42
I've been trying to really solidify my ability to use pointers... so im trying to combine to strings.
I have it working for the most part. The only problem is that text like this: "<<<<<<<<IYIYIYI" always shows up at the end. It's always the same symbols, but not always the same number of symbols. Heres the code:

At first i thought that when i was adding the second string, i was passing the wrong pointer to the array, but when i change the for loop to look like this

for(a=0;a<string2length-4;a++)
{finalstrin[a+string1length]=*(string2+a);}

then the string is four characters shorter, and the wierd characters are still there on the end of the string. I think i might be using dbText wrong.
prasoc
15
Years of Service
User Offline
Joined: 8th Oct 2008
Location:
Posted: 19th Feb 2009 11:58
couldn't you use #include <strings>?
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 19th Feb 2009 12:18
You need to allocate your char array to the length of both of your strings combined +1 as you need the zero terminator. Once done you then need to write your zero terminator. Also I'd advise you not use dbLen() or any GDK string function for that matter, just use strlen() directly. Also the way you're adding them makes no sense, you create a struct that stores both the c-style string and its length yet your function requires you to pass this data separately, why not just overload the + and/or += operator in your struct to make your life easier? Then the way you copy them makes little sense too, why not just use memcpy()?

AlexI
19
Years of Service
User Offline
Joined: 31st Dec 2004
Location: UK
Posted: 19th Feb 2009 15:13 Edited at: 19th Feb 2009 15:14
Try this


I havent tested the code, but it should work

Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 19th Feb 2009 16:06
Quote: "couldn't you use #include <strings>?"

Standard strings are much slower and less efficient

Quote: "and the wierd characters are still there on the end of the string"

Most of the time C strings should be null terminated, so the character: '\0' should be at the end so that commands like dbText know that the character before this null terminator is the last character to print.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 19th Feb 2009 16:23
Quote: "I havent tested the code, but it should work"


You didn't initialize 'combined'.

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 19th Feb 2009 16:36
Quote: "Standard strings are much slower and less efficient"

I wouldn't say they are much slower, but I would say they are a lot safer.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 19th Feb 2009 19:56
Quote: "Standard strings are much slower and less efficient"

Really? I have a 'level playingfield' benchmark of adding strings together that shows otherwise.

Unless you are planning on passing around constant hard-coded strings and not manipulating them in any way, then the std::string class is 9 times out of 10 going to be faster than raw string access.

Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 19th Feb 2009 21:00 Edited at: 19th Feb 2009 21:00
I agree that some standard string operations are equally quick as C string operations. However, several operations are not e.g. changing individual elements of a standard string is significantly slower:


For me, this takes 453ms for CStr and 1765ms for STDStr.

Surely if used as efficiently as possible C strings can't be slower than standard strings?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 19th Feb 2009 22:13 Edited at: 19th Feb 2009 22:13
So how often do you change individual characters within a string? Don't you more often extract substrings, combine strings and search strings?

I still stand by this:
Quote: "the std::string class is 9 times out of 10 going to be faster than raw string access"

You can always find exceptions, but how often do they happen for real?

Now in actual fact, the combining of 2 strings into a third, I can code faster in C than in C++, but the result is 13 lines of code as opposed to the 3 equivalent lines in C++:


As soon as I start passing things around though, the string class starts to win again ... purely because I'm constantly having to get the string length almost every time I use the string - that's why the 'fast' C version wins in the above code (by removing the implicit strlen within the strdup call), and it's the only reason.

Quote: "Surely if used as efficiently as possible C strings can't be slower than standard strings?"

Sure, but look at the code I had to use to do it! Awful stuff.

In my view, unless you have absolutely proven that your string handling is the true bottleneck to your code, you just don't want to go there.

Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 22nd Feb 2009 08:46


Done

Thanks. I've learned alot doing searches related to these topics.

Login to post a reply

Server time is: 2024-09-30 19:16:00
Your offset time is: 2024-09-30 19:16:00