当前位置: 动力学知识库 > 问答 > 编程问答 >

c - postincrement in Left side

问题描述:

I was under the impression that postincrement(OR preincrement) can be done only on right Hand side of equal(=). But I am able to compile below piece of code.

Can you help me in understanding this specific code especially below line.

source : http://www.ibm.com/developerworks/library/pa-dalign/

*data8++ = -*data8;

void Munge8( void *data, uint32_t size ) {

uint8_t *data8 = (uint8_t*) data;

uint8_t *data8End = data8 + size;

while( data8 != data8End ) {

*data8++ = -*data8;

}

}

网友答案:

So, I'm fairly sure that this is undefined behavior. There is no sequence point other than the final semicolon in:

    *data8++ = -*data8;

If data8 is equal to 0x20, is this equal to:

    *(0x20) = -*(0x20);

or

    *(0x20) = -*(0x24);

Because there is no way to make this decision, (because you've edited a variable while reading it twice, with no interleaving sequence point), this is undefined behavior.


We can talk about what the following piece of code does though. Which is likely what is intended by the above code.

while( data8 != data8End ) {
    *data8 = -*data8;
    data8++;
}

What you're doing here is hopefully more straightforward. You're taking your input array, and looking at it so it's a series of 8 bit numbers. Then, in-place, you negate each one.

网友答案:

Your impression is wrong, I guess. You can definitely do things like:

*a++ = *b++;

In fact, that's often how strcpy is implemented. You can even have the post- or pre-increments without an = at all:

a++;
网友答案:

++ is applied to the pointer, not to the value pointed by data8.

*data8++ = -*data8;

is equivalent to:

*data8 = -*data8;
data8++;

EDIT:
After reading the C99 standard 6.5 and Annex C, it's clear = is not a sequence point. Standard mentions only &&, ||, , and ?.

Since, data8 is modified on both sides of = which no sequence point and standard doesn't mandate whether RHS should be evaluated first Or LHS should be evaluated first, I am convinced this is Undefined Behaviour.

Any good reason why assignment operator isn't a sequence point?

This above post discusses = being not a sequence point and is quite related here.

网友答案:

There's no reason why post-increment cannot be done on the left-hand side of the assignment operator. A post-increment operator simply returns a temporary copy of the object in its previous state, and that temporary object (in this case a pointer) can have operations performed on it.

Now, in the case of:

*data8++ = -*data8;

because of operator ordering, the data8 pointer will first be post-incremented, returning a copy of the previous pointer value. That previous pointer value will then be dereferenced and assigned the results of the expression on the right-hand side of the assignment operator.

Edit: As others have pointed out, you're modifying the contents of data8 by reading more than once while writing to that memory location without a sequence point, so this is undefined behavior.

网友答案:

I think that

*data8++ = -*data8;

is equivalent to

*data8 = - *(data8 + 1);

data8 = data8 +1

分享给朋友:
您可能感兴趣的文章:
随机阅读: