Test your skills (2006)
Author |
Message |
OneOffDriveByPoster
|
Posted: Sat Jul 15, 2006 11:27 am Post subject: (No subject) |
|
|
wtd wrote: It only produces undefined behavior if you dereference it. My code never does that. :)
That was fast...
There is a limitation to pointer arithmetic: for any object, you can only go "one past the end" of it and still have it work the way you expect it to.
Of course, dereferencing the "one past the end" produces undefined behaviour. :) |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Sat Jul 15, 2006 11:31 am Post subject: (No subject) |
|
|
Indeed. I'm headed out in a bit, but until then I'm hanging out on the forums and in IRC.
And all I need to do is go one past the end of the array, so that I have a known sentinel value. |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Sat Jul 15, 2006 11:35 am Post subject: (No subject) |
|
|
wtd wrote: And all I need to do is go one past the end of the array, so that I have a known sentinel value.
Thus
so that you don't go past "one-past". |
|
|
|
|
|
wtd
|
Posted: Sat Jul 15, 2006 11:38 am Post subject: (No subject) |
|
|
Ok... I think you're missing something here.
Pointers are just numbers. Within the range of that integer type, I can assign any value I want to a pointer. As long as I don't dereference it, there's no problem. |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Sat Jul 15, 2006 11:40 am Post subject: (No subject) |
|
|
wtd wrote: Ok... I think you're missing something here.
Pointers are just numbers. Within the range of that integer type, I can assign any value I want to a pointer. As long as I don't dereference it, there's no problem.
Everything on a computer are just numbers :)
Just do a Google search on "C++ pointer-arithmetic one-past-the-end". |
|
|
|
|
|
Aziz
|
Posted: Sat Jul 15, 2006 11:43 am Post subject: (No subject) |
|
|
This noobie knows something doesn't it feel good? |
|
|
|
|
|
wtd
|
Posted: Sat Jul 15, 2006 12:34 pm Post subject: (No subject) |
|
|
As far I can tell, the standard specifies only that any of this becomes an issue when dereferencing is brought into question. Comparing the value of two pointers does not entail any dereferencing operation(s).
If you know of a section of the standard that specifies otherwise, please enlighten me. |
|
|
|
|
|
[Gandalf]
|
Posted: Sat Jul 15, 2006 12:37 pm Post subject: (No subject) |
|
|
Aziz wrote: Trying to follow to question as much as possible...
Quote: You may not use "sizeof" or hard-code the size of the array. In other words, it must still work if the size of the array changes.
You're not using any Java equivalent of "sizeof" (which wouldn't really help you anyway) by using myArray.length, nor does it cause problems when the size of the array changes. It's still only checking for the length of the array that was passed, which does not change.
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Aziz
|
Posted: Sat Jul 15, 2006 12:47 pm Post subject: (No subject) |
|
|
I guess I should learn C++ then eh |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Sat Jul 15, 2006 12:49 pm Post subject: (No subject) |
|
|
wtd wrote: As far I can tell, the standard specifies only that any of this becomes an issue when dereferencing is brought into question. Comparing the value of two pointers does not entail any dereferencing operation(s).
If you know of a section of the standard that specifies otherwise, please enlighten me.
5.7 [expr.add] paragraph 5; hope this helps.
Also: I'm sorry for not pm'ing the stuff to you instead of posting--I did not read the 2005 TYS thread until now. |
|
|
|
|
|
wtd
|
Posted: Sat Jul 15, 2006 1:48 pm Post subject: (No subject) |
|
|
If "enum" is being used in reference to all integral types, then pointer arithmetic is smply outright undefined behavior in all forms. |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Sat Jul 15, 2006 2:07 pm Post subject: (No subject) |
|
|
wtd wrote: If "enum" is being used in reference to all integral types, then pointer arithmetic is smply outright undefined behavior in all forms.
? are we looking at the same thing? |
|
|
|
|
|
wtd
|
Posted: Sat Jul 15, 2006 2:15 pm Post subject: (No subject) |
|
|
Can you provide an exact quotation for what you're looking at? |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Sat Jul 15, 2006 2:36 pm Post subject: (No subject) |
|
|
wtd wrote: Can you provide an exact quotation for what you're looking at?
Here it is:
ISO C++98 5.7.5 wrote: When an expression that has integral type is added to or subtracted
from a pointer, the result has the type of the pointer operand. If
the pointer operand points to an element of an array object, and the
array is large enough, the result points to an element offset from the
original element such that the difference of the subscripts of the
resulting and original array elements equals the integral expression.
In other words, if the expression P points to the i-th element of an
array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N
(where N has the value n) point to, respectively, the i+n-th and i-n-
th elements of the array object, provided they exist. Moreover, if
the expression P points to the last element of an array object, the
expression (P)+1 points one past the last element of the array object,
and if the expression Q points one past the last element of an array
object, the expression (Q)-1 points to the last element of the array
object. If both the pointer operand and the result point to elements
of the same array object, or one past the last element of the array
object, the evaluation shall not produce an overflow; otherwise, the
behavior is undefined. |
|
|
|
|
|
wtd
|
Posted: Sat Jul 15, 2006 2:49 pm Post subject: (No subject) |
|
|
Gracias.
Interesting. Of course, having the ability to go one past the end suffices for pretty much any foreseeable scenario. |
|
|
|
|
|
|
|