macro trick?

Discussion in 'C Programming' started by P L, Oct 17, 2003.

  1. P L

    P L Guest

    I have codes with blocks like this:

    void h() {
    ..
    ..
    ..
    a;
    b1;
    b2;
    ..
    ..
    ..
    bn;
    c;
    ..
    ..
    ..
    }

    and I need to change it to something like:

    static void g()
    {
    b1;
    b2;
    ..
    ..
    ..
    bn;
    }

    void h() {
    ..
    ..
    ..
    a;
    #ifdef ABC
    b1;
    b2;
    ..
    ..
    ..
    bn;
    #else
    f(g); /* f is some function taking g as arg */
    #endif
    c;
    ..
    ..
    ..

    }

    I don't like to copy the blocks because of two reasons: there are lot
    of such changed need to be made; and any future changes in b1; ...; bn
    will need changes in two places. I am reluctant to add a lot of
    backslashes and put b1; ...; bn in a macro; and would prefer some
    other elegant way. Can someone shed some light? Thanks. - PL
     
    P L, Oct 17, 2003
    #1
    1. Advertising

  2. P L

    Kris Wempa Guest

    "P L" <> wrote in message
    news:...
    > I have codes with blocks like this:
    >
    > void h() {
    > .
    > .
    > .
    > a;
    > b1;
    > b2;
    > .
    > .
    > .
    > bn;
    > c;
    > .
    > .
    > .
    > }
    >
    > and I need to change it to something like:
    >
    > static void g()
    > {
    > b1;
    > b2;
    > .
    > .
    > .
    > bn;
    > }
    >
    > void h() {
    > .
    > .
    > .
    > a;
    > #ifdef ABC
    > b1;
    > b2;
    > .
    > .
    > .
    > bn;
    > #else
    > f(g); /* f is some function taking g as arg */
    > #endif
    > c;
    > .
    > .
    > .
    >
    > }
    >
    > I don't like to copy the blocks because of two reasons: there are lot
    > of such changed need to be made; and any future changes in b1; ...; bn
    > will need changes in two places. I am reluctant to add a lot of
    > backslashes and put b1; ...; bn in a macro; and would prefer some
    > other elegant way. Can someone shed some light? Thanks. - PL



    Your explanation of what you need to do is extremely confusing. Can you
    explain it a little better ?
     
    Kris Wempa, Oct 17, 2003
    #2
    1. Advertising

  3. P L

    P L Guest

    My apology for the confusion. The problem arose from a bug in a large
    software to be fixed. I determined that the bug could be fixed by
    modifying existing code like this:

    void h() {
    .
    .
    .
    a;
    b1; /* this block will be duplicated in the fix */
    b2;
    .
    .
    .
    bn;
    c;
    .
    .
    .
    }


    to something like this:

    static void g()
    {
    b1; /* 1, the first copy of the block */
    b2;
    .
    .
    .
    bn;
    }

    void h() {
    .
    .
    .
    a;
    #ifdef ABC
    b1; /* 2, the second copy of the block */
    b2;
    .
    .
    .
    bn;
    #else
    f(g); /* f is some function taking g as arg */
    #endif
    c;
    .
    .
    .
    }

    As you can see, the change included two duplicated copies of the same
    block b1; ...; bn. I don't like it because there are lot of such
    changed need to be made and any future changes in b1; ...; bn will
    need changes in two places. One way I have to avoid duplicating the
    blocks is

    static void g();

    void h() {
    .
    .
    .
    a;
    #define BLOCK
    b1; \
    b2; \
    . \
    . \
    . \
    bn;
    #ifdef ABC
    BLOCK
    #else
    f(g); /* f is some function taking g as arg */
    #endif
    c;
    .
    .
    .
    }


    static void g()
    {
    BLOCK /* 1, the first copy of the block */
    }
    #undef BLOCK



    Yet I am reluctant to do this and add a lot of backslashes and would
    prefer some elegant alternative. Thanks. -PL
     
    P L, Oct 20, 2003
    #3
  4. P L

    P L Guest

    Another alternative without those back slashes is:

    static void g()
    {
    b1;
    b2;
    ...
    bn;
    }

    void h()
    {
    /* block A of code */

    #ifdef ABC
    g();
    #else
    f(g); /* f is some function taking g as arg */
    #endif

    /* block C of code */
    }

    Yet that would move the block b1, b2, ..., bn away from where it
    currently is. Can someone suggets a better alternative that keeps b1,
    b2, ..., bn where it is? Thanks. -PL
     
    P L, Oct 20, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dead RAM
    Replies:
    20
    Views:
    1,132
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    590
    Jack Klein
    Sep 21, 2003
  3. Mac
    Replies:
    15
    Views:
    483
    Kay Schluehr
    Jun 5, 2005
  4. Chris Thomasson

    Simple Macro Function Pointer Trick...

    Chris Thomasson, Sep 14, 2007, in forum: C++
    Replies:
    2
    Views:
    2,946
    Chris Thomasson
    Sep 17, 2007
  5. ld

    macro trick

    ld, Aug 5, 2009, in forum: C Programming
    Replies:
    5
    Views:
    1,330
Loading...

Share This Page