leave C++ strings. get on the char* boat.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

leave C++ strings. get on the char* boat.

Post by avansc »

here is some code to help you. i know alot of people have trouble with "strings" pointer to a character.

is easy making a string in C.

Code: Select all

 char *name = "andre";
but i i have noticed people struggle to change C string values without causeing memory problems.

Code: Select all


char *newStr(char *str)
{
	char *temp = (char*)malloc(strlen(str)*sizeof(char));
	temp = str;
	return temp;
}

you can use this to assign new data to an new or old char* variable

Code: Select all

char *name = "andre";
name = newStr("jack");
when ever you are done with a pointer in C, always free it.

Code: Select all

free(name);
there are many str functions in C, strcmp() strcat() and so on. there are your friends, they are faster than C++ gaybo functions.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Trask
ES Beta Backer
ES Beta Backer
Posts: 738
Joined: Wed Oct 29, 2008 8:17 pm
Current Project: Building a 2D Engine
Favorite Gaming Platforms: Sega Genesis and Xbox 360
Programming Language of Choice: C/C++
Location: Pittsburgh, PA
Contact:

Re: leave C++ strings. get on the char* boat.

Post by Trask »

You know I appreciate your postings, I feel like a better programmer for reading them sometimes... I gotta start copying them out of this forum in case I wake up and it's gone one day. Thanks for the examples!
MarauderIIC wrote:You know those people that are like "CHECK IT OUT I just made Linux run on this piece of celery [or other random object]!!"? Yeah, that's Falco, but with ES.
Dear god, they actually ported ES to a piece of celery!
Martin Golding wrote: "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: leave C++ strings. get on the char* boat.

Post by avansc »

Trask wrote:You know I appreciate your postings, I feel like a better programmer for reading them sometimes... I gotta start copying them out of this forum in case I wake up and it's gone one day. Thanks for the examples!
np mate. just trying to spread the knowledge.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
bugmenot
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Sun Dec 07, 2008 7:05 pm

Re: leave C++ strings. get on the char* boat.

Post by bugmenot »

avansc wrote: there are many str functions in C, strcmp() strcat() and so on. there are your friends, they are faster than C++ gaybo functions.
Personally, I much prefer the robustness of the C++ string object over error prone C string libraries as it means I can spend more time actually programming rather then fixing memory related bugs.

Also, not all C string library functions are faster then the C++ counterparts. One example is working out the string length. Depending on the implementation, strlen will iterate through each character of a string until it hits '\0' to work out the length of a string whereas the C++ string object will just return its cached value.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: leave C++ strings. get on the char* boat.

Post by avansc »

bugmenot wrote:
avansc wrote: there are many str functions in C, strcmp() strcat() and so on. there are your friends, they are faster than C++ gaybo functions.
Personally, I much prefer the robustness of the C++ string object over error prone C string libraries as it means I can spend more time actually programming rather then fixing memory related bugs.

Also, not all C string library functions are faster then the C++ counterparts. One example is working out the string length. Depending on the implementation, strlen will iterate through each character of a string until it hits '\0' to work out the length of a string whereas the C++ string object will just return its cached value.
yeah i can tell its you first post.

firstly, C String library is bug free. secondly, the reason you have so many memory related bugs is because you dont program enought.....
lastly, i dont think using strln as major c string function is a good example. if you wanna go that route, everytime C++ strings updates the string you end up having to update that cashe value, wasting time, and resources.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
bugmenot
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Sun Dec 07, 2008 7:05 pm

Re: leave C++ strings. get on the char* boat.

Post by bugmenot »

I agree the C string library is bug free, however it is far more acceptable to usage errors then the C++ string library and because it requires memory allocation management on the part of the user, it can lead to subtle errors such as buffer overruns and memory stomps which can be extremely hard to debug if the crash happens and the debugger points you at a line of code nowhere near the actual cause.

In addition, having to keep track of how long your char buffer needs to be to store a particular string or any string that gets passed in can be extremely troublesome (at this point, I would suggest using the strn* version of the string functions to prevent buffer overruns).

There is also an error with your newStr function as it only copies the pointer address of char *, not the string data it is pointing to and it the memory it allocates should be 1 more then the string length to store the NULL terminator. It should be:

Code: Select all

char *newStr(char *str)
{
   char *temp = (char*)malloc((strlen(str)+1)*sizeof(char));
   strcpy( temp, str );
   return temp;
}
Also, it ideally needs an assert to check for NULL pointers.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: leave C++ strings. get on the char* boat.

Post by avansc »

^ i'll have to check on that. im not sure about that.
you make good points though.

yeah your right. i kinda feel imbarrased i made such a big mistake. i think i copied code that was just inline that worked. but yeah, my func def as it is does not work. thanks for pointing that out.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
bugmenot
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Sun Dec 07, 2008 7:05 pm

Re: leave C++ strings. get on the char* boat.

Post by bugmenot »

avansc wrote:i think i copied code that was just inline that worked.
I would go back and check that code as the code you have written in that function is also leaking memory:

Code: Select all

   char *temp = (char*)malloc(strlen(str)*sizeof(char));
   temp = str;
The address to the memory that was allocated has now been lost as both temp and str are pointing to the same string and neither are pointing to the malloc'ed memory.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: leave C++ strings. get on the char* boat.

Post by avansc »

bugmenot wrote:
avansc wrote:i think i copied code that was just inline that worked.
I would go back and check that code as the code you have written in that function is also leaking memory:

Code: Select all

   char *temp = (char*)malloc(strlen(str)*sizeof(char));
   temp = str;
The address to the memory that was allocated has now been lost as both temp and str are pointing to the same string and neither are pointing to the malloc'ed memory.
i check it, it was fine. i was just lazy/forgetful when i copied in here.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Slacker
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 31
Joined: Tue Nov 11, 2008 11:41 am
Location: MountLake Terrace, WA
Contact:

Re: leave C++ strings. get on the char* boat.

Post by Slacker »

Wow... I had the hardest time with C strings. I remember in my C class I glazed over (read: slept through) several of the MANY lectures on strings, and boy was that a mistake. I spent more time pounding my head against the keyboard with string related errors than I'd like to remember.
~Slacker
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: leave C++ strings. get on the char* boat.

Post by Falco Girgis »

I will say that if any of you plan to do any assembly programming at any point, being good at this stuff is going to make your live soooo much easier.

The one thing that my high school C programming class was good for was learning good ol 'character array manipulation...
User avatar
sparda
Chaos Rift Junior
Chaos Rift Junior
Posts: 291
Joined: Tue Sep 23, 2008 3:54 pm

Re: leave C++ strings. get on the char* boat.

Post by sparda »

A cool thing about modern compilers is how they optimize everything. For instance, MSVS uses the same address space for identical data, in the case of c strings. All of the variables below point to the same address, as the code would show:

Code: Select all

#include <stdio.h>
#include <stdlib.h>

 char *name   = "yes";
 char *name2  = "yes";
 char *name3 = "yes";
int main(void)
{
	printf("address:%p, address2:%p, address3:%p", name, name2, name3);
	return EXIT_SUCCESS;
}
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: leave C++ strings. get on the char* boat.

Post by MarauderIIC »

sparda wrote:For instance, MSVS uses the same address space for identical data, in the case of c strings.
That's pretty sweet. I didn't know that.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
sparda
Chaos Rift Junior
Chaos Rift Junior
Posts: 291
Joined: Tue Sep 23, 2008 3:54 pm

Re: leave C++ strings. get on the char* boat.

Post by sparda »

Thanks Marauder. Like I say: "you learn something everyday" ;)

I've picked this up from messing around with pointers and stuff. I was trying to write a memory manager not too long ago for a game I'm working on (I'll get back to it when school is done in about a week and a half).

There are a slew of hidden little facts like these in my ebook collection (I know you frown about this stuff Marauder, so sorry ahead of time) ;) Come to think about it, I'm in the process of uploading all my stuff onto an ftp server of mine. If you guys want I'll post a link to it when I'm done uploading; its taking forever though, since my hosting service limits the upload bandwidth to 60kb/s, fucking fucks!

On that note, anyone know of a hosting service that doesn't limit upload bandwidth for ftp? I'm using hostmonster for my web-side stuff.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: leave C++ strings. get on the char* boat.

Post by MarauderIIC »

sparda wrote:(I know you frown about this stuff Marauder, so sorry ahead of time) ;)
Only to keep the site from being subject to cease and desists and/or revocation of service.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply