Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Test your skills (2006)
Index -> General Programming
Goto page Previous  1, 2, 3, 4, 5, 6  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
OneOffDriveByPoster




PostPosted: 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
Sponsor
sponsor
wtd




PostPosted: 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




PostPosted: 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
code:
args + (argc - 1)

so that you don't go past "one-past".
wtd




PostPosted: 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




PostPosted: 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




PostPosted: Sat Jul 15, 2006 11:43 am   Post subject: (No subject)

This noobie knows something Razz doesn't it feel good?
wtd




PostPosted: 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]




PostPosted: 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.
Smile
Sponsor
Sponsor
Sponsor
sponsor
Aziz




PostPosted: Sat Jul 15, 2006 12:47 pm   Post subject: (No subject)

I guess I should learn C++ then eh Razz
OneOffDriveByPoster




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: Sat Jul 15, 2006 2:15 pm   Post subject: (No subject)

Can you provide an exact quotation for what you're looking at?
OneOffDriveByPoster




PostPosted: 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




PostPosted: 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.
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 5 of 6  [ 77 Posts ]
Goto page Previous  1, 2, 3, 4, 5, 6  Next
Jump to:   


Style:  
Search: