3 ms·
The first block of code seems more readable to me than the second. I'm used to seeing the first; it's idiomatic C. I don't understand why it says "do ... while
by drivers99 13y ago
The first block of code seems more readable to me than the second. I'm used to seeing the first; it's idiomatic C. I don't understand why it says "do ... while (0)" in the macro, for instance. Wouldn't you want to handle different errors in different ways anyway? In your example, you're just going to leave the file open and the string allocated? Isn't trying to influence how the compiler compiles it by using a macro instead of a function a clear example of (premature) optimization?
- theseoafs 13y ago> The first block of code seems more readable to me than the second. I'm used to seeing the first; it's idiomatic C. The benefits of the macro (IMO) aren't obvious from the example I gave. Like I said, I'm currently working on a personal project where macros have been the saving grace. Attaching a conditional return to every individual statement was making my code extremely hard to maintain (even for myself, who wrote it) -- wrapping common error cases in a macro made everything a whole lot simpler, and allowed me to work on my algorithms at a high level without having to run my eyes over the same error-checking `if` statements a million times. > I don't understand why it says "do ... while (0)" in the macro, for instance. It's normal for many macros, so that you can use them like normal statements. > In your example, you're just going to leave the file open and the string allocated? I sort of just made that example up off the top of my head -- you're right about the memory leak.
- nitrogen 13y agoRegarding do ... while(0): http://stackoverflow.com/questions/1067226/c-multi-line-macro-do-while0-vs-scope-block http://stackoverflow.com/questions/1067226/c-multi-line-macr...