Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

what does this do:  #define CSVERIFY(f) ((void)(f))     ?

Posted on 2007-08-04
12
Medium Priority
?
419 Views
Last Modified: 2013-11-05
I am working with a 3rd party API that often uses the following #define,

#define CSVERIFY(f) ((void)(f))

An example of how they use it is:

CSVERIFY( SUCCESS == GetByName( dataStruct, FIELD_NAME, &myInt ));

In english: the dataStruct holds several name/value pairs and we are using the GetByName function to get the data associated with the field FIELD_NAME and put it in the variable myInt. We are testing the output of this function to determine if it was able to get the data correctly.  

Then the mysterious CSVERIFY ?????

Any ideas?

thanks
0
Comment
Question by:highqllc
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 19630874
#define CSVERIFY(f) ((void)(f))

actually just removes the test condition by expanding the macro to nothing bit it's argument. It is similar to

#ifdef  NDEBUG

#define assert(exp)     ((void)0)

#else

#define assert(exp) (void)( (exp) || (_assert(#exp, __FILE__, __LINE__), 0) )

#endif  /* NDEBUG */
0
 

Author Comment

by:highqllc
ID: 19630935
But why did they do this??  They must have had a good reason...  

I want to understand their reasoning for doing this so I can determine if I should use this macro when I add to their code, if it's OK for me to not use it, etc.

thanks
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 19630970
The reason is that there is another definition of that macro somewhere else that expands differently. With

#define CSVERIFY(f) ((void)(f))

the line

CSVERIFY( SUCCESS == GetByName( dataStruct, FIELD_NAME, &myInt ));

simply means

SUCCESS == GetByName( dataStruct, FIELD_NAME, &myInt );

and the compiler makes that a call to

GetByName( dataStruct, FIELD_NAME, &myInt );

without checking the return value.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:highqllc
ID: 19631013
ahh... ok, I did see that there was another definition for it if _DEBUG was defined. So, basically they use it as an internal debugging mechanism, and I should be OK not using it in any code I've written using their API (assuming I don't want to use their debugging features)?
0
 
LVL 86

Expert Comment

by:jkr
ID: 19631036
>>I should be OK not using it in any code I've written using their API (assuming I
>>don't want to use their debugging features)?

In that case, yes. Just add your own error checking.
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 200 total points
ID: 19631127
>>and I should be OK not using it in any code I've written using their API

I highly recommend against using this macro, since it's poorly worded.
In most libraries, VERIFY is similar to ASSERT, but with the difference that it evaluates the expression in the release version of the code.
This macro does not evaluate the expression in the release version, so it's very misleading, and can lead to unexpected behavior, if a developer is not aware of it.
0
 
LVL 86

Expert Comment

by:jkr
ID: 19631152
>>I highly recommend against using this macro, since it's poorly worded.

???

It does not matter if they name it 'CSVERIFY' or 'Bubba', as long as they *document* it well enough to not let questions like this one arise.
0
 
LVL 30

Expert Comment

by:Axter
ID: 19631162
>>It does not matter if they name it 'CSVERIFY' or 'Bubba', as long as they *document* it well enough to not let questions like this one arise.

When you have a good coding naming conversion, you don't have to go searching the document to figure out what the code is doing.
Moreover, you should always try using common practice, so that it's easier for developers to pickup on a new library.

This would be like creating a string class, and calling it an Int_Class.  And then claiming it's OK to do so, as long as you document it.

Documentation is no substitute for good coding practice.
0
 
LVL 86

Expert Comment

by:jkr
ID: 19631177
It might fit in the naming convention scheme of the company that created the code in question. It's a little easy on the triger to call that  "bad practise" without knowing the background or even knowing what library that code is part of. But that is off-topic anyway...
0
 
LVL 30

Expert Comment

by:Axter
ID: 19631199
>>t fit in the naming convention scheme of the company that created the code in question. It's a little easy on the triger to call that  "bad practise" without
>>knowing the background or even knowing what library that code is part of. But that is off-topic anyway...

The internal workings of the company is not a factor.  This is especially so, when the interface is being exposed publicly.

Any public interface should conform to normal coding standards, regardless of the documentation.

0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 19631400
Specifically they do this so that the compiler will not complain about these statements that do nothing.

Another example as to why I use (void) in front of some statements is:

say I create a function x(int y, int z)

in the function I have used y but, I have not gotten around to using z or maybe I no longer need z. The compiler will issue a warning stating that z is not used.

If I add the statement:
(void)z;  

This will quiet the warning and alert me the next time I read through the code that I am no longer using z in the body of the code.

I hope this answers your question.

Some additional ideas with regard to CSVERIFY.
Have you found anywhere where CSVERIFY is re-defined to assert like jkr is suggesting. The reason they define it to ((void)(f)) is because they wanted the statement to compile to "nothing". But, my guess (which is the same as jkr's) is that at development time they probably replaced the define with assert(f).

Check out these links (I tried to find links that explain the use of assert macros):
http://www.codeguru.com/forum/showthread.php?t=315371
http://www.ddj.com/cpp/184403861;jsessionid=BME5YPM34WZY2QSNDLPSKHSCJUNN2JVN?_requestid=168307

The use of assert calls is an excellent software development tool.
0
 
LVL 30

Expert Comment

by:Axter
ID: 19631417
>> I have not gotten around to using z or maybe I no longer need z. The compiler will issue a warning stating that z is not used.

FYI:
You can also quiet the compiler warnings just by not naming the unused argument.
Example:
int  foo (int y, int  /* z*/ )  //Comment out the NAME of the unused argument.

This will stop the compiler from complaining about it, but still let you have the extra argument in the function declaration.
This method also lets anyone know right away by looking at the top of the function implementation, that this argument is not used.

Here's a link for a commonly used VERIFY macro.
http://msdn2.microsoft.com/en-us/library/fcatwy09(VS.80).aspx
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question