Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » strcpy is being deprecated????

This thread is locked; no one can reply to it. rss feed Print
 1   2 
strcpy is being deprecated????
Carrus85
Member #2,633
August 2002
avatar

KC: Yeah, I should have caught that. I did state untested though :P (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
avatar

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
avatar

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
avatar

Also nobody seemed to get the parameters right, it ischar* dest, const char* src :)

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Paul Pridham
Member #250
April 2000
avatar

const is for wimps. ;)

HoHo
Member #4,534
April 2004
avatar

... and for compiler that wants to optimize as much as possible ;)

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

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
avatar

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.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

ImLeftFooted
Member #3,935
October 2003
avatar

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
avatar

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;
}

;D

Paul Pridham
Member #250
April 2000
avatar

Congratulations! You've just added 2MB to the executable. ;)

HoHo
Member #4,534
April 2004
avatar

Quote:

Congratulations! You've just added 2MB to the executable. ;)

1#include <algorithm>
2 
3char* strncpy(char* dest, const char* src, const size_t len) {
4 std::copy(src, src+len, dest); // hooray for the STL!
5 *(dest+len-1) = 0;
6 return dest;
7}
8 
9int main(){
10 char *str1=new char[10], *str2=new char[10];
11 strncpy(str2, str1, 9);
12 return 0;
13}
14 
15#g++ test.cpp
16#ls -l a.out
17-rwxr-xr-x 1 hoho users 7061 Mar 10 02:04 a.out

If in doubt, benchmark ;)

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Joel Pettersson
Member #4,187
January 2004

Quote:

You've found smart compilers? Where? Are they less then $10000 a license? *

  • This is a joke. Compilers are very far from perfect.

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
avatar

-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
avatar

Try compiling with -O2 -ffast-math -fomit-frame-pointer

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

HoHo
Member #4,534
April 2004
avatar

Better with -Os and maybe with -fomit-frame-pointer :)
Also a mingw und UPx compressing might be interesting.

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 :)

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Goalie Ca
Member #2,579
July 2002
avatar

Another silly discussion but...

string copy(src)
copy.c_str();

-------------
Bah weep granah weep nini bong!

Jakub Wasilewski
Member #3,653
June 2003
avatar

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!".

---------------------------
[ ChristmasHack! | My games ] :::: One CSS to style them all, One Javascript to script them, / One HTML to bring them all and in the browser bind them / In the Land of Fantasy where Standards mean something.

CGamesPlay
Member #2,559
July 2002
avatar

I like to think the community has just moved away from it-is-so-because-guru-says-it-is mysticism.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

ImLeftFooted
Member #3,935
October 2003
avatar

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
avatar

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
avatar

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 ;D

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

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 


Go to: