开发者

A problem in C/C++ programming

开发者 https://www.devze.com 2023-03-31 11:38 出处:网络
I have to two loops with different conditions, but the code in loops is same. Like this: for (xx; xxxx; xxx)

I have to two loops with different conditions, but the code in loops is same.

Like this:

for (xx; xxxx; xxx)
{
    the Code
}

for (yy; yyyy; yyy)
{
    the Code
}

The problem is the开发者_JAVA百科 code is too long and I want to write the code only one time and execute these two different loops.

How can I solve it?


Maybe you should put that long block of code in a function/method and invoke the function/method in the two loops?

type blockOfCode() {
    block of code...
    return type; //Or if it's is void nothing obviously.
}

for (xx, xxx, xxxx)
    blockOfCode();
for (yy, yyy, yyyy)
    blockOfCode();


Why don't you make it a function? That way you'll have one copy of the code and any changes / bug fixes you make would be reflected in both places.

An inline function would make it shorter but in terms of effects on the icache, its still going to be as long as your code.


One solution would be to put the code into a (n inline) function so it's shorter:

inline function a() { /* stuff */ }

for (x; xx; xxx) a();
for (y; yy; yyy) a();


you need to refactor the code to move the common code (in the for loops) into one function


For completeness, and only if you are completely sure you can't use a function, then there is a nasty (emphasis on nasty!) fallback; the (justifiably) dreaded macro:

#define TheCode(p1) \
    do { \
        line1...; \
        line2...maybe using p1...; \
        line3...; \
    } while (0)

for (xx; xxxx; xxx)
{
    TheCode(x);
}

for (yy; yyyy; yyy)
{
    TheCode(y);
}

This is categorically not the recommended solution; using a function is superior. But as an act of desparation, it might solve some problem. On the whole, though, if you can't use a function, you've got problems with your existing code that you should solve before you fall back on this as an alternative.


How about putting the Code in a function that you'd call from inside each loop? Could that work for you?


Well, it's really hard to know exactly what you will need here, without a little more context as to what the conditions of your two for statements are, or some clue as to how any differences in your conditions appear within the loops. And, I definitely would much prefer to refactor it into another function rather than do something hacky to just make it work, since there are very likely many more issues with it than just being repeated if things are that complex.

But, if I had to do something like this, and I very much recommend against it if you have any way to avoid it, but you can do this (or some variation on it depending on your particular code and the nature of the for conditions):

change this:

original code

// ... some code 

for (int i=0; i < 5; ++i)
{
    // loop code  loop code  loop code
    // loop code  loop code  loop code
    // loop code  loop code  loop code
    // loop code  loop code  loop code
    // loop code  loop code  loop code
}

// ... more code

for (int j=11; j < 20; ++j)
{
    // loop code  loop code  loop code
    // loop code  loop code  loop code
    // loop code  loop code  loop code
    // loop code  loop code  loop code
    // loop code  loop code  loop code
}

// ... rest of code

to this:

modified code

// ... some code

for (int i=0; i < 5; ++i)
{
    #include "badforloop.i"
}

// ... more code

for (int i=11; i < 20; ++i)
{
   #include "badforloop.i"
}

// ... rest of code

[NOTE: if, instead of the loop variable being i in both loops, it was different variables, you should modify them so they are the same.]

new file: badforloop.i

    // loop code  loop code  loop code
    // loop code  loop code  loop code
    // loop code  loop code  loop code
    // loop code  loop code  loop code
    // loop code  loop code  loop code
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号