|
|
Comments and Discussions
|
|
 |

|
This article came at the right time while I was writing this article(below).
http://www.codeproject.com/string/StringFormatNative.asp
I was thinking how to support the original C functions and the new Safe C functions of VC8 in the same source files. The #defines is a timely help. Thanks!
|
|
|
|
|

|
THIS ARTICLE IS NOT ABOUT ELIMINATING WARNINGS IN VS2005!
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|

|
If you eliminate the warnings you could hide other problems that you won't know about until they bite you on the arse.
I try and think of a warning as the compiler saying that it will compile the code, but there exists some problem which you should really clear up. I actually elevate some warnings to errors becuase I know that they will cause some real problems in the future if it isn't resolved. By problems, mostly I mean inadvertant bugs at run time. I'm a great believer in getting the compiler to catch as many problems as possible.
|
|
|
|

|
It depends upon what you are doing. For example, I work on cross platform programs where the other compilers and libraries use the more standard naming convention. So, the options are either to disable the warning (I prefer the defines method as opposed to the pragma option) or to use a system similar to John Simmon's article. We have no interest in building alternative function call wrappers - if we do not have to. And Microsoft supplied a way to avoid the warning because they understood the implications of the name change.
|
|
|
|

|
Jason King wrote: It depends upon what you are doing.
You're right - of course. Everything should be examined on its merits. My general rule is not to disable any warnings. But having said that today put an email round the development team where I work with a good case for disabling one of the warnings Visual Studio Team System Code Analysis was producing. (So, now I've just realised that I've shot my own argument in the foot - damn!) It isn't something I do lightly because for the most part warnings are there for a good reason.
|
|
|
|

|
If it's your own code throwing the warning, there are *very* few valid reasons to just pragma around it.
But like I said before, that's *not* what this article is about.
This article is about sharing code with legacy MS compilers which don't have the new prototypes.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|

|
Cosidering the declaring the CRT functions deprecated for safety is a MS initiative that is controversed in the C++ community, and the MS itself is rethinking about, why not just disable the useless warnings ?
It's easy:
#pragma warning(disable: 4100) #pragma warning(disable: 4996) #pragma warning(disable: 4512) #pragma warning(disable: 4345)
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|

|
emilio_grv wrote: why not just disable the useless warnings ?
Because disabling warnings could potentially mask other subtle problems in your code. Granted, there are some warnings that simply aren't addressable via source code, and using pragma to get a clean compile is necessary, but this is most certainly not one of those cases.
Also:
1) This article is more for the VS2005 guys that need to share source code with VC6 guys.
2) This article is NOT about getting rid of warnings in VS2005.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|

|
I'm sorry, but I don't get the point:
Which subtle bug should I loose if I disable the C4996 that is a specific warning about CRT (That its up to me to decide to use) intruduced by Microsoft only and only in C++8 that will probably disappear in C++8.1 ?
If I'm warned about something is perfectly normal, i'm not warned. Just spammed(!)
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|

|
THIS ARTICLE IS NOT ABOUT ELIMINATING WARNINGS IN VS2005!
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|

|
emilio_grv wrote: (That its up to me to decide to use)
That is exactly the point, it's up to YOU to decide. Some of us other programers would also like the choice.
Personaly I only ever disable a warning if I feel there is no other way, even when doing so, I re-enable the warning directly after the line in question. Making a habit of this will not only help you find bugs, but help eliminate them in the first place.
|
|
|
|

|
Yep, that's exacly my point:
to stay with John (the author), the situation is:
There is a thing (CRT) in VC6 that has a function named strcpy
That same thing, in VC8 is still named strcpy. But the compiler is instructed to say “thst’s “deprecated” (note the the use of the wor d”deprecated in incorrecti in this sense)
Another new function, named strcpy_s (but with different parameters) had been introduced.
My point is: the two function are not one the replacement fort the other. They are different, since they suppose I have a different way to track buffer lengths.
I can agree that the second is safer than the first, but I cannot use the second where the code was not supposed to use it.
Now, John (correct me if I misunderstood) says: I told my team, when developing NEW code, to code respecting the “_s” paradigm, and –when they are required to use old platforms- I give them some wrappers that expose the new prototype, but fall through the old implementation. (In fact , he use two distinct implementation of a same wrapper). So my coders will always take care of the buffer length. Simply, the old platform doesn’t do the checks.
What I’m saying is a different thing:
Someone wants me to know that a more safe function exist to do a job. I know it, but I want –for some reason- to still use the old one, for example, when porting the old code in the new implementation.
In that case, more than a wrapper, I just need do disable the warning, since the problem it’s not the code (whose correctness is supposed to be proven), but the warning itself (that’s talking about something is not required, in that context), warning me about a problem that I know for sure it doesn’t exist.
Hence, I disable it and re-enable it when the point when I’m sure about is over.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|

|
The only thing missing from the article is Visual Studio Version values (as I'm too lazy to look them up ).
What is the version number for VS6.0, VS2002, VS2003 & VS2005?
I realize this is slightly out of scope for the article but your article is all about dealing with different versions of Visual Studio & its related complier.
Little hunting around the MSDN here is then version info
_MSC_VER
1400 = VS 8.0 / VS 2005
1310 = VS 7.1 / VS 2003
1300 = VS 7.0 / VS 2002
1200 = VS 6.0
-- modified at 4:57 Monday 20th November, 2006
I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:
|
|
|
|

|
Within the context of this article, the other version numbers don't matter. MSC versions prior to 1400 don't have the "secure" prototypes defined.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|

|
John Simmons / outlaw programmer wrote: Within the context of this article, the other version numbers don't matter.
Fair enough.
I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:
|
|
|
|

|
#ifdef _VS2005_
int sprintf_ex(char* sDest, int nSize, char* sFormat, ...)
{
va_list argList;
va_start(argList, sFormat);
int nCount = 0;
nCount = _vsnprintf_s(sDest, nSize, nSize-1, sFormat, argList);
va_end(argList);
return nCount;
}
#else
#define sprintf_ex _vsnprintf(sDest, nSize, sFormat argList);
#endif
above should be more performant (for VS 2003)
but maybe one would prefer something like:
#ifndef _VS2005_ //not deffed? add function
int _vsnprintf_s(char* sDest, int nSize,int doesnotexistin2003, char* sFormat, ...)
{
va_list argList;
va_start(argList, sFormat);
int nCount = 0;
nCount = _vsnprintf(sDest, nSize, sFormat argList);
va_end(argList);
return nCount;
}
#endif
first example 'spoils' only VS2005 with some 'overhead', second one 'only' 2003 (there migth be still something left to optimize
|
|
|
|

|
That's certainly another way to do it.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|

|
I have found the _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES macro which does this work in all possible cases. Is it less worth for this task?
Greetings from Germany
|
|
|
|

|
I didn't do this as much for me (using VS2005) as I did for the other team using VC6. It's an admitted pain in the ass, but I'd rather write code that compiles clean without the need for shortcut macros. To each his own. You certainly aren't required to use this technique, and there have been a couple of other methods suggested in other messages.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|

|
I gave you a five just for the Disclaimers!
|
|
|
|

|
Instead of wrapping _vsnprintf_s/_vsnprintf, you could wrap _vsntprintf_s/_vsntprintf. Christian wrote a bunch of such wrapper functions when we released Ultimate Toolbox 2005 - because the code has to compile on VC 6, VC 2003, and VC 2005.
|
|
|
|

|
Oh btw, nice article. Though it could have done with a screenshot
|
|
|
|

|
I was going to post a screen shot of my avatar, but I couldn't find one big enough to make an impression.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|

|
Like I said, expanding on the idea is a minor exercise for most C++ programmers.
In our specific implementation, we're not using unicode so it honestly didn't occur to me.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
A technique for making stdio functions compile clean regardless of the Microsoft compiler used
| Type | Article |
| Licence | |
| First Posted | 17 Nov 2006 |
| Views | 63,428 |
| Bookmarked | 10 times |
|
|