Page 1 of 2
leave C++ strings. get on the char* boat.
Posted: Thu Dec 04, 2008 6:24 pm
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.
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.
there are many str functions in C, strcmp() strcat() and so on. there are your friends, they are faster than C++ gaybo functions.
Re: leave C++ strings. get on the char* boat.
Posted: Thu Dec 04, 2008 6:32 pm
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!
Re: leave C++ strings. get on the char* boat.
Posted: Thu Dec 04, 2008 6:46 pm
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.
Re: leave C++ strings. get on the char* boat.
Posted: Sun Dec 07, 2008 7:13 pm
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.
Re: leave C++ strings. get on the char* boat.
Posted: Sun Dec 07, 2008 7:21 pm
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.
Re: leave C++ strings. get on the char* boat.
Posted: Sun Dec 07, 2008 8:19 pm
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.
Re: leave C++ strings. get on the char* boat.
Posted: Sun Dec 07, 2008 8:43 pm
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.
Re: leave C++ strings. get on the char* boat.
Posted: Mon Dec 08, 2008 7:14 am
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.
Re: leave C++ strings. get on the char* boat.
Posted: Mon Dec 08, 2008 9:41 am
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.
Re: leave C++ strings. get on the char* boat.
Posted: Mon Dec 08, 2008 11:26 am
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.
Re: leave C++ strings. get on the char* boat.
Posted: Mon Dec 08, 2008 11:43 am
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...
Re: leave C++ strings. get on the char* boat.
Posted: Mon Dec 08, 2008 12:20 pm
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;
}
Re: leave C++ strings. get on the char* boat.
Posted: Mon Dec 08, 2008 12:25 pm
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.
Re: leave C++ strings. get on the char* boat.
Posted: Mon Dec 08, 2008 12:45 pm
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.
Re: leave C++ strings. get on the char* boat.
Posted: Mon Dec 08, 2008 3:49 pm
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.