Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use #define inside a function?

Tags:

c++

c

For example, I saw source code like the following. Can we use #define in a function? How does it work? (more information: this code is what I copied from openvswitch source code):

void *
ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
{
switch (code) {
case OFPUTIL_ACTION_INVALID:

#define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
#include "ofp-util.def"
    OVS_NOT_REACHED();

#define OFPAT10_ACTION(ENUM, STRUCT, NAME)                  \
case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
#define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)      \
case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)        \
case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
#include "ofp-util.def"
}
OVS_NOT_REACHED();
}

#define OFPAT10_ACTION(ENUM, STRUCT, NAME)                        \
void                                                        \
ofputil_init_##ENUM(struct STRUCT *s)                       \
{                                                           \
    memset(s, 0, sizeof *s);                                \
    s->type = htons(ENUM);                                  \
    s->len = htons(sizeof *s);                              \
}                                                           \
                                                            \
struct STRUCT *                                             \
ofputil_put_##ENUM(struct ofpbuf *buf)                      \
{                                                           \
    struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
    ofputil_init_##ENUM(s);                                 \
    return s;                                               \
}
#define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
OFPAT10_ACTION(ENUM, STRUCT, NAME)
#define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
OFPAT10_ACTION(ENUM, STRUCT, NAME)
#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)            \
void                                                        \
ofputil_init_##ENUM(struct STRUCT *s)                       \
{                                                           \
    memset(s, 0, sizeof *s);                                \
    s->type = htons(OFPAT10_VENDOR);                        \
    s->len = htons(sizeof *s);                              \
    s->vendor = htonl(NX_VENDOR_ID);                        \
    s->subtype = htons(ENUM);                               \
}                                                           \
                                                            \
struct STRUCT *                                             \
ofputil_put_##ENUM(struct ofpbuf *buf)                      \
{                                                           \
    struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
    ofputil_init_##ENUM(s);                                 \
    return s;                                               \
}
#include "ofp-util.def"
like image 450
peng xu Avatar asked May 01 '16 15:05

peng xu


People also ask

How do you use it is possible?

It was possible that Alex was making no progress with her because she actually didn't want what she was asking for. It didn't seem possible. It was possible after all!

Is it possible or is this possible?

In a statement, the verb comes between: “It is possible”, “It is very possible”. (“Very” is the adjective modifying “possible”, so it has to be next to it.) In a question, the verb comes first: “Is it possible?”, “Is it even possible?”

Could it be possible or would it be possible?

Could, would, and should are all used to talk about possible events or situations, but each one tells us something different. Could is used to say that an action or event is possible. Would is used to talk about a possible or imagined situation, and is often used when that possible situation is not going to happen.

Is it correct to say if it is possible?

If the situation is one of impossibility or hypothesis, you should say If it were possible. If it is one of probability or likelihood, say If it was possible.


1 Answers

#define is a preprocessor directive: it is used to generate the eventual C++ code before it is handled to the compiler that will generate an executable. Therefore code like:

for(int i = 0; i < 54; i++) {
  #define BUFFER_SIZE 1024
}

is not executed 54 times (at the preprocessor level): the preprocessor simply runs over the for loop (not knowing what a for loop is), sees a define statement, associates 1024 with BUFFER_SIZE and continues. Until it reaches the bottom of the file.

You can write #define everywhere since the preprocessor is not really aware of the program itself.

like image 166
Willem Van Onsem Avatar answered Sep 22 '22 16:09

Willem Van Onsem