0

I need to use strncpy_s in my code. The code needs to be compiled using msvc71 as well as msvc90 compilers. It turns out that strncpy_s is not supported in msvc71. I have implemented my own version of strncpy_s

So I want to compile this implementation of strncpy_s only when the compiler is msvc71. How can I do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Abhash Kumar Singh
  • 1,157
  • 2
  • 12
  • 22
  • 2
    There is a macro to identify the compiler – Iharob Al Asimi Jan 26 '16 at 14:08
  • 3
    [`_MSC_VER` et al](https://msdn.microsoft.com/en-us/library/b0084kay.aspx) – Igor Tandetnik Jan 26 '16 at 14:09
  • If you have your own version, just use it all the time. – Pete Becker Jan 26 '16 at 14:12
  • 1
    You can still use `strncpy` in msvc90. This is cleaner then using a conditional compilation. – fuz Jan 26 '16 at 14:13
  • @PeteBecker What? that's not a good idea. – Iharob Al Asimi Jan 26 '16 at 14:13
  • 1
    @iharob - why not? If the user's version works correctly it works correctly. – Pete Becker Jan 26 '16 at 14:14
  • Because the default implementation will necessarily be more efficient, unless the user implements it like the Microsoft C Runtime does. – Iharob Al Asimi Jan 26 '16 at 14:15
  • @FUZxxl - **dont't** use `strncpy` until you've read its documentation carefully and understood it. It is **not** a drop-in "safe" replacement for `strcpy`. – Pete Becker Jan 26 '16 at 14:15
  • 1
    @iharob - um, if the user's version isn't a bottleneck for the old compiler, there's no reason to assume it will be a bottleneck for the new compiler. Swapping implementations around can, however, be a bottleneck for development. If it ain't broke, don't fix it. – Pete Becker Jan 26 '16 at 14:18
  • 1
    @PeteBecker What can I say, "*If it ain't broke, don't fix it*" is the voice of experience so I agree. – Iharob Al Asimi Jan 26 '16 at 14:20
  • 1
    @FUZxxl: `strncpy` and `strncpy_s` have different semantics. Suggesting `strncpy` is a bad idea. Its behavior is obscure and counterintuitive. Even if you know exactly what it does, most programmers reading your code will not and will think it is a *cool* replacement for `strcpy`. – chqrlie Jan 26 '16 at 14:32
  • 1
    @PeteBecker Of course. If available, I recommend the use of `strlcpy`. – fuz Jan 26 '16 at 14:35

1 Answers1

3

Using conditional compilation is not a very good solution to your problem: if you later compile with another compiler or on a different platform, you will need to add more and more special tests to handle these environments that may or may not support strncpy_s. There is a standard way to request for these extended functions and to check whether they are available, but I strongly recommend you define your replacement function at all times, name it something else and use it unconditionally.

As you may be aware, strncpy and strncpy_s are not interchangeable, they have different semantics, beyond the extra argument for the size of the destination. strncpy definitely should not be used, because it is error prone: even if you master its peculiar side effects, other programmers later reading or modifying your code will not. strncpy_s has its own quirks: the behavior on constraint violation may not be what you expect.

chqrlie
  • 131,814
  • 10
  • 121
  • 189