7

The title is a bit vague as I don't really know how to define this question.

It has to do with the following code:

for (match         = root,
     m_matchBase   = match->requestedBase,
     m_matchLength = match->length;

     match != NULL;

     match         = match->next,
     m_matchBase   = match->requestedBase,
     m_matchLength = match->length)
{
    if (m_matchBase <= m_base && m_matchBase + m_matchLength > m_base)
        break;
    // other stuff...
}

Are the statements in the for loop guaranteed to run sequentially?

For example, is m_matchBase = match->requestedBase guaranteed to run after match = match->next?

John Dibling
  • 99,718
  • 31
  • 186
  • 324
Jimmy Lu
  • 4,810
  • 7
  • 25
  • 30
  • 1
    Yes it's guaranteed , [further informations](http://stackoverflow.com/questions/54142/how-does-the-comma-operator-work) – Mostafa 36a2 Jan 17 '14 at 19:08
  • http://stackoverflow.com/questions/52550/what-does-the-operator-do-in-c#52558 – clcto Jan 17 '14 at 19:08
  • Is this C or C++? Pick one. Since you mentioned only "C" in the title, for now I'm going to assume you meant what you said, and I'll edit your tags appropriately. – John Dibling Jan 17 '14 at 19:10
  • i would have said 'the expressions in the for statement' – pm100 Jan 17 '14 at 19:10
  • http://stackoverflow.com/questions/276512/what-is-the-full-for-loop-syntax-in-c-and-others-in-case-they-are-compatible – pm100 Jan 17 '14 at 19:11
  • 1
    @JohnDibling, I think this applies to both C and C++, shouldn't it? These 2 languages should agree with each other on this kind of fundamental stuff I think. – Jimmy Lu Jan 17 '14 at 19:16
  • @BeyondSora: Maybe true, but clarification from OP would be nice. – John Dibling Jan 17 '14 at 19:17
  • Which language are you programming in? C and C++ are different languages and unless you have a specific reason you want to compare the two it is usually a bad idea to tag with both. – Shafik Yaghmour Jan 17 '14 at 19:34
  • 1
    @ShafikYaghmour, I'm aware they're different languages. However, for this particular question, I think it applies to both languages (something that C and C++ both should agree on). – Jimmy Lu Jan 17 '14 at 19:39
  • Well, it is very confusing since your title specifically says *C*, so you should remove *C* from the title and make it clear you care about both cases. – Shafik Yaghmour Jan 17 '14 at 19:58

4 Answers4

8

Yes, the comma operator (which is what is being used here) will sequence the operations. This means that your loop will quite likely crash when match->next becomes null.

Mark B
  • 95,107
  • 10
  • 109
  • 188
5

The expressions will be evaluated from left to right and there will be a sequence point after each evaluation. In C the grammar for a for statement without a declaration from the draft C99 standard section 6.8.5 Iteration statements is:

for ( expressionopt ; expressionopt ; expressionopt ) statement

So the , in each set of expressions will be a comma operator as opposed to just a separator, which means the assignments will be evaluated from left to right. This is covered in section 6.5.17 Comma operator which says:

The left operand of a comma operator is evaluated as a void expression; there is a sequence point after its evaluation. Then the right operand is evaluated; the result has its type and value

Whether this is maintainable code is another question, it is important to note that when match>next returns NULL you will be invoking undefined behavior in subsequent sub-expressions. Which probably goes some way to demonstrate this is a poor choice in style since it is easy to miss and hard to check in the current form.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
3

Yes, see c++11 standard (5.18):

A pair of expressions separated by a comma is evaluated left-to-right; the left expression is a discarded- value expression

tumdum
  • 1,981
  • 14
  • 19
2

Yes.

The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.

The && operator also has a sequence point.

Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62