Of course, developers are always bad guys when it comes to errors :). It could be argued that developers are to blame for these errors, not macros. By the way, it was detected in the StarEngine project. I think the reader realizes that it was quite an inappropriate macro. How about such a reduction in writing the std::printf function? #define sprintf std::printf This error from Midnight Commander is described here. As a result, it turned out that one of the conditions was always true and the code worked not as intended. The developer, who was using the isspace thought that he was using the actual function, which considers not only spaces and tabs as space characters, but also LF, CR and some others. For example, the substitution of the isspace function with the following macro: #define isspace(c) ((c)=' ' || (c) = '\t') I’m continually describing such cases in my articles. Moreover, bugs related to macros, are often very difficult to notice when reviewing code. I don’t know how to explain the reasons for this phenomenon from a philosophical point of view, but it is so. Let’s see some motives to avoid such macros. I’m talking about other macros, which developers use to avoid implementing a full function or try to reduce the function size. Indeed, they have been created to simplify programming. It makes no sense to fight against something like this: BEGIN_MESSAGE_MAP(efcDialog, EFCDIALOG_PARENT ) ![]() For example, I put things lightly to the macros in old programs written with usage of MFC. ![]() For example, when it comes to the manual generation of similar code, I can recognize the benefits of macros and deal with them. It should be noted, however, that I’m not a fanatic and I don’t appeal to abandoning macros out of idealistic considerations. So let’s try to use macros as seldom as possible! The C++ language opens extensive opportunities to go without macros. You can find him online on Twitter and Facebook Andrey promotes methodologies of static and dynamic code analysis. ![]() He is the author of a large number of articles, dedicated to code quality and telling about different patterns of errors that C++ programmers make. Andrey is a Microsoft MVP in the “Developer Technologies” category and one of the founders of the PVS-Studio project. Today, I am happy to announce a guest post by Andrey Karpov about the evil of macros.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |