Jump to content
  • Advertisement
Sign in to follow this  
ph33r

strncpy is declared deprecated

This topic is 4728 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So was sprintf and all there related functions, is there any C alternatives I can use?

Share this post


Link to post
Share on other sites
Advertisement
If you are writing in C++, use C++.


#include <string>
#include <iostream>
using namespace std;

int main() {
string x = "y helo thar";
// implicitly invoked constructor: std::string(const char*)

string y;

y = x; // copies the string as per str*cpy-type functions

y[5] = 'l'; // omg I make dirty wr0d :(

cout << x << endl << y << endl;
}


If you are writing in C, consult your compiler's documentation to determine how to make sure it understands that you want to write in C.

Share this post


Link to post
Share on other sites
There is absolutely no reason not to use char arrays or pointers for string usage even if your code is native C++. If all you need to do is keep ahold of some text or have an easily accessed buffer without all the overhead of an entire data structure, I would recommend char arrays every time.

If you want to do lots of concatenation and inserting and stuff like that, then you might want to look into std::string or some other implementation of a string wrapper, but to say that using chars is not a good idea is spreading misinformation.

Added ESPECIALLY if you're going to expose the whole std namespace just to use the string class!

Share this post


Link to post
Share on other sites
Quote:
Original post by ph33r
So was sprintf and all there related functions, is there any C alternatives I can use?


Okay people, before we get into a holy war over C++ strings, take a look at the original post. ph33r is asking for a C function.

Who has said that these functions are deprecated? Not that they shouldn't be for security reasons, but there's no standard cross-platform replacement yet.

If you really want to avoid strncpy but stay within the standard library, you could use strncat, just set the first character of the destination array to 0 and it should work just like strncpy.

Share this post


Link to post
Share on other sites
Quote:
Original post by ph33r
So was sprintf and all there related functions, is there any C alternatives I can use?


Who says they're deprecated? If it's your compiler, tell it you're programming in C, not C++ (e.g. end your files with .c instead of .cpp or .C, or give gcc the option "-x C", or whatever). If it still says they're deprecated, it's probably prefering some platform specific extension (OpenBSD prefers strlcpy(), etc. which are not standard).

So, your options:
Use non-standard functions of C-strings (e.g. strlcpy())
Use non-standard strings in C (e.g. make a struct string { char *str; size_t len; }; sort of thing)
Use the "deprecated" C functions (What most C programmers do)
Use another language

Share this post


Link to post
Share on other sites
It's pretty obvious where your getting those deprecated messages its from VC++ 8.0. Its not deprecated from the C or C++ standard as of yet only in VC++ 8.0 compiler they are deprecated to be completely replaced by the secure, safe versions yes there is a safe version of strncpy its:


strncpy_s


Yes you can use it now if you want.

All C I/O routines are deperecated and have safe/secure versions, they end with _s, these are being considered to be standardized in C and i think C++ too, read more about it Security Enhancements in the CRT and The Safe C library.

[Edited by - snk_kid on June 13, 2005 2:44:15 PM]

Share this post


Link to post
Share on other sites
Thank you snk_kid, strncpy_s is what I was looking for. That article was an interesting read as well.

Share this post


Link to post
Share on other sites
Quote:
Original post by Omaha
There is absolutely no reason not to use char arrays or pointers for string usage even if your code is native C++.


Wrong



1) If you use any function which has to find the end of the string (std::strlen, std::strcat, etc) there's a good to perfect chance std::string will outpreform since it stores the string's length. This changes appending a series of data to a string from an O(N*N) to an O(N) complexity operation - see "Joel on Software - Back to Basics" for a full explaination and history about the dreaded painters algorithm Shlemiel the painter's algorithm.

2) std::string manages it's memory. Randomly malloced char pointer? Welcome to memory leak city...

3) a = b + c; - works with std::string, causes a to point to a random memory location with char pointers. The C version?

a = malloc( strlen( b ) + strlen( c ) + 1 );
strcpy( a , b );
strcat( a , c );


That's no less than 3 lines of code, AND it uses the painters algorithm (which is bad, so there's a good chance "a = b + c" will outpreform this version), AND we're omitting the free( a ) statement which we must call or suffer a memory leak.

Also note that the first time I wrote that I accidentally missed the + 1, which meant it suffered a buffer overflow. ANOTHER reason not to use char pointers.

[Edited by - MaulingMonkey on June 13, 2005 2:28:24 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Omaha
Added ESPECIALLY if you're going to expose the whole std namespace just to use the string class!


It was just an example...I'm sure he's aware he could have just exposed the string class...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!