From: Doug Harrison [MVP] on 28 Apr 2010 15:43 On Wed, 28 Apr 2010 09:53:59 +0200, "Giovanni Dicanio" <giovanniDOTdicanio(a)REMOVEMEgmail.com> wrote: >I think that the usefulness of the safe string functions is that they always >put a NUL-terminator in the destination buffer (so even if the programmer >forgets to check the return value, at least the destination buffer contains >a well-formed string, yes truncated, but at least NUL-terminated). But if he forgets to check the return value, he could go on to delete C:\Windows instead of C:\Windows2. :) This is one reason I prefer the CRT-level functions, which crash the program instead of letting it continue with corrupt data. The compiler really should force one to check the return value for functions like StringCchCopy, which are hardly "safe" otherwise. It actually has this capability when you throw the /analyze switch, but the functions haven't been marked up for their return values, just their parameters. Then again, if you faithfully check return values, your string-handling code could end up looking like a COM monstrosity. To avoid that, you can validate everything at the start and thus guarantee the end result will fit, but then you're back to good C practice and don't really need the "safe" functions. <good example snipped> >But, after this discussion, the following question comes in my mind: "Why is >the OP using these C-style string functions, instead of using a convenient >string class like CString??" >(To my knowledge, these days the only reason to use pure C - at least in the >Windows world - is writing kernel-mode device drivers...) Yep. -- Doug Harrison Visual C++ MVP
From: Joseph M. Newcomer on 28 Apr 2010 18:37 #ifndef _countof #define _countof(x) (sizeof(x)/ sizeof((x)[0])) #endif It is that easy! Note the observation about templates made below. Now your code is compatible with all versions of the compiler, whereas if you move to a newer compiler your code will fail because it thinks you are redefining an existing macro! joe On Wed, 28 Apr 2010 17:46:10 +0200, "Giovanni Dicanio" <giovanniDOTdicanio(a)REMOVEMEgmail.com> wrote: >"RB" <NoMail(a)NoSpam> wrote: > >> Thanks Giovanni for all of the < cut out > elaboration, it helps me to >> further get under the concept which will enable me to program more >> correctly in any circumstance (or LIB used ). > >RB: You are welcome. > >> My version of VS does not have the _countof( ) routine (macro ? ) >> but I found this on a search, which I surmise would enable the me >> the same product (with less typing and code line length). >> #define CountOf(array) (sizeof(array)/sizeof(array[0])) > >Probably you are using something older than VS2005 (a.k.a. VC8). > >Yes, _countof() can be defined using preprocessor macro like you did above. >But if you are using C++ (not pure C) and a modern C++ compiler that >understands templates well, then you can use a template metaprogramming >trick to make _countof() fail if a pointer is passed as argument, instead of >a static array, as described here: > >http://groups.google.com/group/microsoft.public.vc.mfc/msg/3f20f126165d6a3f > >Giovanni > Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: RB on 28 Apr 2010 20:28 > I don't suppose it would inconvenience you to show us the line that generate the error? > How is it we are supposed to make sense of an error message when you don't show > us the line it occurred on? > I suspect the line has a sprintf in it. > Yes it was a sprintf that the error was flagged on. Ugh well, sorry Joe, I would post it now but I deleted it yesterday when I replaced it with StringCchPrintf. It did not have an error persay, in other words the code compiled fine prior to inserting a StringCchCopy function. What really made me curious was how the compiler knew to tell me that, which has been answered already. But another curious thing was I had the strsafe.h included several compiles prior to actually inserting the StringCchCopy function and I did not get the error "suggestion". I only got the said error after I inserted the StringCchCopy function, which I'm sure there is a reason for it, not sure what it is.
From: RB on 28 Apr 2010 20:35 > But if you are using C++ (not pure C) and a modern C++ compiler that understands templates well, then you can use a template > metaprogramming trick to make _countof() fail if a pointer is passed as argument, instead of a static array, as described here: > http://groups.google.com/group/microsoft.public.vc.mfc/msg/3f20f126165d6a3f Thanks Giovanni, I can see that a pointer would a misconceived size, and I will copy this link. But I have never really done the template thing so will have to read on that first.
From: RB on 28 Apr 2010 20:38 Thanks Joe, I copied this and the link. With the help of this group I may actually write more bullet prove code as opposed to the paper bridge my earlier code rested on.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: CRichEditCtrl contents from RTF file Next: Error, Windows Cannot find hcw file |