|
This thread is locked; no one can reply to it. |
1
2
|
strcpy is being deprecated???? |
Carrus85
Member #2,633
August 2002
|
KC: Yeah, I should have caught that. I did state untested though (I should have checked len's validity, then done *(dest-1) = 0 as dest is already past the end at that point...)
|
ImLeftFooted
Member #3,935
October 2003
|
Quote: I think Microsoft was deprecating the versions without lenght to prevent bugs. Maybe thats how the idea came up. But it ended up being more of a way to lock people into WINBLOWS specific code and make porting away from windows harder. |
Paul Pridham
Member #250
April 2000
|
void strcopy(char* dest, char* src) { while( *dest++ = *src++ ); } It's efficient, but isn't compatible with strcpy() since it doesn't return the original dest. That is all. ---- |
HoHo
Member #4,534
April 2004
|
Also nobody seemed to get the parameters right, it ischar* dest, const char* src __________ |
Paul Pridham
Member #250
April 2000
|
const is for wimps. ---- |
HoHo
Member #4,534
April 2004
|
... and for compiler that wants to optimize as much as possible __________ |
Joel Pettersson
Member #4,187
January 2004
|
HoHo said: ... and for compiler that wants to optimize as much as possible For the "smarter" ones, it often makes no difference.;)
|
ReyBrujo
Moderator
January 2001
|
Quote: Maybe thats how the idea came up. But it ended up being more of a way to lock people into WINBLOWS specific code and make porting away from windows harder. Actually, I would bet the amount of strn.* functions in the Linux kernel overwhelms the str[^n]+ ones. At least, that is what I would expect. -- |
ImLeftFooted
Member #3,935
October 2003
|
Quote: For the "smarter" ones, it often makes no difference.;) You've found smart compilers? Where? Are they less then $10000 a license? * * This is a joke. Compilers are very far from perfect. |
Carrus85
Member #2,633
August 2002
|
Quote: char* dest, const char* src Well, of course. Here, lets make it complete: char* strcpy(char* dest, const char* src) { char* temp = dest; while( *dest++ = *src++ ); return temp; } Or, better yet: #include <algorithm> char* strncpy(char* dest, const char* src, const size_t len) { std::copy(src, src+len, dest); // hooray for the STL! *(dest+len-1) = 0; return dest; }
|
Paul Pridham
Member #250
April 2000
|
Congratulations! You've just added 2MB to the executable. ---- |
HoHo
Member #4,534
April 2004
|
Quote: Congratulations! You've just added 2MB to the executable.
If in doubt, benchmark __________ |
Joel Pettersson
Member #4,187
January 2004
|
Quote: You've found smart compilers? Where? Are they less then $10000 a license? *
Hence the quotes around and usage of "smarter" rather than "smart". I know all too well; my recent experiences with the way GCC reacts to and handles x87 (ye olde register-stack-based x86 FPU) inline assembly makes me feel like hand-coding all my DSP code in assembly.
|
Paul Pridham
Member #250
April 2000
|
-rwxrwxrwx+ 1 Administrators None 248 Mar 9 22:46 c_version.c* -rwxr-xr-x 1 ppridham None 7691 Mar 9 22:47 c_version.exe* -rwxrwxrwx+ 1 Administrators None 335 Mar 9 22:47 stl_version.cpp* -rwxr-xr-x 1 ppridham None 41201 Mar 9 22:47 stl_version.exe* Huh. Pretty bloated in Cygwin it seems. ---- |
ReyBrujo
Moderator
January 2001
|
Try compiling with -O2 -ffast-math -fomit-frame-pointer -- |
HoHo
Member #4,534
April 2004
|
Better with -Os and maybe with -fomit-frame-pointer Carrus85 version of strncpy (no optimizations, -Os): -rwxr-xr-x 1 hoho users 6644 Mar 10 10:48 a.out -rwxr-xr-x 1 hoho users 6604 Mar 10 10:48 a.out libc strncpy: -rwxr-xr-x 1 hoho users 6525 Mar 10 10:50 a.out -rwxr-xr-x 1 hoho users 6517 Mar 10 10:50 a.out I wondeor how it is possible that libc's strncpy creates smaller executable than the function Carrus gave Seems to me as there isn't much difference between C and C++, not at least under Linux __________ |
Goalie Ca
Member #2,579
July 2002
|
Another silly discussion but... string copy(src) copy.c_str();
------------- |
Jakub Wasilewski
Member #3,653
June 2003
|
When did A.cc become the Royal Benchmarking Society? Some of the threads have become kind of like Programmer Mythbusters . "Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!". --------------------------- |
CGamesPlay
Member #2,559
July 2002
|
I like to think the community has just moved away from it-is-so-because-guru-says-it-is mysticism. -- Ryan Patterson - <http://cgamesplay.com/> |
ImLeftFooted
Member #3,935
October 2003
|
The community has grown wiser |
Billybob
Member #3,136
January 2003
|
Quote: I like to think the community has just moved away from it-is-so-because-guru-says-it-is mysticism. Using the ++ operator makes your programs larger.
|
ImLeftFooted
Member #3,935
October 2003
|
Quote: Using the ++ operator makes your programs larger. How very wise. Another little known fact is that for statements also make your programs larger! |
HoHo
Member #4,534
April 2004
|
Quote: I like to think the community has just moved away from it-is-so-because-guru-says-it-is mysticism. I'd like to think that people benchmark more and guess less Jakub Wasilewski, thanks for my new sig __________ |
runderwo
Member #8,430
March 2007
|
The posted strncpy() replacement also violates the semantics of strncpy, namely that the destination buffer is padded up to the length if the source is shorter. And it's invalid if called with length 0. There's no reason to reinvent these functions except for better portability... because doing so creates problems like this that lurk, just waiting to be exploited in released code. |
|
1
2
|