Jump to content

Talk:Increment and decrement operators: Difference between revisions

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
Content deleted Content added
Assessment: Computing: class=C, importance=Low, software=y, software-importance=Mid (assisted)
No edit summary
Line 6: Line 6:


:I added a paragraph about undefined behavior, which should probably cover that specific point. — [[User:Loadmaster|Loadmaster]] ([[User talk:Loadmaster|talk]]) 18:34, 16 June 2011 (UTC)
:I added a paragraph about undefined behavior, which should probably cover that specific point. — [[User:Loadmaster|Loadmaster]] ([[User talk:Loadmaster|talk]]) 18:34, 16 June 2011 (UTC)

==Incorrect definition==
The article states that the "increment operator increases the value of its operand by 1", but this is only sometimes correct, and misses the point of the operator. The operator actually increases the value by an amount that reflects the number of machine words required to store the data. So if it's a character pointer on a byte-oriented machine, it increments by 1. If it's a pointer to a 4-byte int, then it increments by 4. If it's a pointer to a float on a machine that represents floating-point numbers in a single word, then it increments by 1, but on a modern byte-oriented machine it might increment it by 8. The point is, the compiler knows how big the items are and increments by the correct amount so the sense of the operation is always "step to the next thing". If the operand is an integer, then +1 steps to the next integer value. The article, as written, ignores the major value the operator has for programs that use pointers and structs.

Revision as of 02:43, 22 February 2018

WikiProject iconComputing: Software C‑class Low‑importance
WikiProject iconThis article is within the scope of WikiProject Computing, a collaborative effort to improve the coverage of computers, computing, and information technology on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
CThis article has been rated as C-class on Wikipedia's content assessment scale.
LowThis article has been rated as Low-importance on the project's importance scale.
Taskforce icon
This article is supported by WikiProject Software (assessed as Mid-importance).

Undefined behavior

Is it worth noting that using pre- and post- increment in the same expression results in undefined behavior in many languages? Thomblake (talk) 16:19, 16 June 2011 (UTC)[reply]

I added a paragraph about undefined behavior, which should probably cover that specific point. — Loadmaster (talk) 18:34, 16 June 2011 (UTC)[reply]

Incorrect definition

The article states that the "increment operator increases the value of its operand by 1", but this is only sometimes correct, and misses the point of the operator. The operator actually increases the value by an amount that reflects the number of machine words required to store the data. So if it's a character pointer on a byte-oriented machine, it increments by 1. If it's a pointer to a 4-byte int, then it increments by 4. If it's a pointer to a float on a machine that represents floating-point numbers in a single word, then it increments by 1, but on a modern byte-oriented machine it might increment it by 8. The point is, the compiler knows how big the items are and increments by the correct amount so the sense of the operation is always "step to the next thing". If the operand is an integer, then +1 steps to the next integer value. The article, as written, ignores the major value the operator has for programs that use pointers and structs.