5

Suppose that I have a requirement that if an operation is performed within a scope, then it must be undone in the same scope. An example of that is entering and leaving a critical section.

In order to enforce users to use paired do - undo operations, a pair of macros is defined that use open and close braces:

#define BEGIN \
{ \
    do_something();

#define END \
    undo_something(); \
}

Of course, there are ways that a "malicious" coder can trick those macros (e.g. by adding a an opening or closing brace), but generally this helps remembering that a BEGIN must be followed by an END. Moreover, if, for example, an existing BEGIN is commented out, the compiler will complain, indicating that the END must be removed also.

I have seen this is several internal projects. As I said, it does not offer 100% protection but could this practice prove harmful in any way? Is this a well-known practice?

3
  • An even amount of mistakes may go undetected. Jan 31, 2017 at 8:23
  • As I said, there are many ways errors can go undetected but I am asking if this can actually be harmful in some way (i.e. produce code in an unexpected manner).
    – Kostas
    Jan 31, 2017 at 8:26
  • why don't you just use the names begin() and end() for do_something and undo_something? Or Begin() and End() if you want them to stand out more and your capitalization finger needs exercise.
    – rici
    Jan 31, 2017 at 16:25

5 Answers 5

4

If I understand the question you want to enforce the use of BEGIN / END.

You can do that using the infamous goto:

#define BEGIN \
do { \
    goto check_label_end; \
label_begin: \
    puts("begin"); \
} while (0);

#define END \
do { \
    puts("end"); \
    break; \
check_label_end: \
    goto label_begin; \
} while (0);

Then, if BEGIN is used without the END part you receive an error:

error: label ‘check_label_end’ used but not defined

EDIT:

As pointed out by @ KlasLindbäck in comments, this version limits the use of BEGIN / END to once per function.

You can pass a label name in order to use several blocks:

#define BEGIN(op) \
do { \
    goto check_label_end_##op; \
label_begin_##op: \
    puts("begin"); \
} while (0);

#define END(op) \
do { \
    puts("end"); \
    break; \
check_label_end_##op: \
    goto label_begin_##op; \
} while (0);

BEGIN(undo)
...
END(undo)

BEGIN(something_else)
...
END(something_else)
4
  • 1
    Nifty. It should be noted that this limits the use of BEGIN/END to once per function. Jan 31, 2017 at 9:33
  • For this to be practical you need a way to generate distinct label names for each usage of the macro in such a way that the intended pairs find each other. I'm not sure if that really reduces the chance to write a bug compared to useing no special technique at all.
    – MikeMB
    Jan 31, 2017 at 9:46
  • 1
    This is a cool trick thanks! The only con I see is that the generated code will make two unnecessary jumps while in BEGIN, unless compilers optimize those out (I think they do not).
    – Kostas
    Jan 31, 2017 at 12:46
  • 1
    After a simple check, it looks that at least ARM gcc DOES optimize the jumps. So this is a 10/10 answer!
    – Kostas
    Jan 31, 2017 at 13:31
2

For simple cases I don't see a problem, but you can construct scenarious, where this breaks your code:

BEGIN
...
for(...) {
    END
    ...
    BEGIN
}
...
END
2

The sensible way to do this is not through macros, but through functions. For example:

typedef void task_t (void);

inline void do_critical_stuff (task_t* task)
{
  enter_critical_section();
  task();
  leave_critical_section();
}

Or if you are looking for something completely variable:

inline void do_stuff (begin_t* begin, task_t* task, end_t* end)
{
  begin();
  task();
  end();
}
0

I don't see any serious problem provided that BEGIN/END are used (and indented) the same way as other blocks.

The obvious caveat is that return/longjmp/goto breaks the pattern, but as long as all coders know this it shouldn't be much of a problem.

Example:

void foo() {
  ...
  BEGIN
    ...
    if (some_condition) {
      return;  // Cleanup code skipped
    }
    ...
  END
  ...
}
1
0
#define FOO(body) {\
    do_something();\
    body\
    undo_something();\
}

You use this macro like:

FOO({
    //your code here
})

--

Edit:

Okay, StoryTeller's example breaks my code so easily in a way I've never seen.

I guess I rarely write multiple declarations in a single line.

Comma operator and function calls never break like a=(1,2); or memcpy(&a,&b,1);

3
  • @StoryTeller switching to __VA_ARGS__ fixes it easily though :)
    – Quentin
    Jan 31, 2017 at 8:32
  • @StoryTeller I wrote a lot of such code. Never have problems for my own hobby projects.
    – cshu
    Jan 31, 2017 at 8:33
  • @cshu - I just linked to an example that breaks with your answer on the simplest body I could imagine. Either you uses it in a limited very fashion, or your answer is incomplete. Jan 31, 2017 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.