Dissecting numbers

This is a discussion on Dissecting numbers within the C++ Programming forums, part of the General Programming Boards category; you could ask the user to enter that number in a character array, and just do - 48. a[0] = ...

  1. #16
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    you could ask the user to enter that number in a character array, and just do - 48.

    a[0] = 1 , to get the number , do a[0] - 48
    a[1] = 2 " "
    a[2 ] = 3, " "

  2. #17
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by epidemic
    you could ask the user to enter that number in a character array, and just do - 48.

    a[0] = 1 , to get the number , do a[0] - 48
    a[1] = 2 " "
    a[2 ] = 3, " "
    Don't use magic constants... If a[0] == '1' to get a number use a[0] - '0'
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I spent some more time thinking about the "proper" way to write forwards and backwards loops. If one wants the loop variable to be local to the loop, this requires a for loop. If instead of an integer loop variable, one has an iterator for a container c, then my loops would be written (leaving out the definition for p):
    Code:
    for (p=c.begin(); p != c.end(); p++) {}
    and
    Code:
    for (p=c.end(); p-- != c.begin();) {}
    but as I've read recently, the standard says that the behavior is undefined if one merely _creates_ an invalid pointer, even if it's never dereferenced - for example see the _long_ thread here:

    http://groups.google.com/group/comp....18f6cccd56427d

    In the second loop above, in the last test, p is set to c.begin() - 1 which is invalid. It turns out that
    Code:
    for (p=c.end()-1; p >= c.begin(); p--) {}
    is invalid for the same reason. Apparently the only correct way to do it is
    Code:
    for (p=c.end(); p != c.begin();) {
      p--;
    // rest of loop
    }
    Although my second loop with the integer variable worked properly, I could have written it similarly as
    Code:
    for (int i=n; i != 0;) {
      i--;
    // rest of loop
    }
    which would be clearer although longer. If the loop was written in Java, the "!= 0" would have to be included in any case.

    One thing confuses me about the creation of invalid pointers. An uninitialized local variable can normally take on any possible value. Does this mean that a local pointer variable HAS to be initialized immediately to avoid having an invalid value? Or is it only if one explicitly assigns an invalid value to it that one has a problem?

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    I would write your loops as:
    Code:
    for (Iterator p = c.begin(); p != c.end(); ++p) {}
    and
    Code:
    for (ReverseIterator p = c.rbegin(); p != c.rend(); ++p) {}
    In the second loop above, in the last test, p is set to c.begin() - 1 which is invalid.
    That is true.

    It turns out that
    Code:
    for (p=c.end()-1; p >= c.begin(); p--) {}
    is invalid for the same reason.
    I disagree. For random access iterators:
    Code:
    p = c.end() - 1;
    must have the same net effect with respect to p as:
    Code:
    p = c.end();
    p--;
    So either they are both invalid, or they are both valid. In my opinion, they are both valid. The C++ standard states that the pre-condition for --r (and thus r--) for some iterator r is that "there exists s such that r == ++s". The post-condition is that "s is dereferenceable". But the pre-condition for ++r is "r is dereferenceable" with the post-condition that "r is dereferenceable or r is
    past-the-end". As far as I can tell, the code snippets above satisfy the pre-conditions.

    An uninitialized local variable can normally take on any possible value. Does this mean that a local pointer variable HAS to be initialized immediately to avoid having an invalid value? Or is it only if one explicitly assigns an invalid value to it that one has a problem?
    I think that there is no problem since it is uninitialised. Of course, it is good practice to initialise a pointer to null in such a case.
    Last edited by laserlight; 01-03-2007 at 08:17 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    What I was referring to was not the validity of
    Code:
    p = c.end() - 1;
    although there actually is another issue there - if c has zero length, then c.end() == c.begin() so even the initial value is invalid. I was thinking of the fact that in the last iteration of the loop, p == c.begin(), and then at the end of the loop, p-- tries to set p to c.begin() - 1. The loop assumes that the next test will fail, but if the code is running on something that checks each pointer assignment and traps immediately if the value is invalid, the program may crash right there. Or there may be some kind of wraparound so the test doesn't fail. The point is, as soon as the invalid pointer is generated, all bets are off.

    Edit: Please replace the word "pointer" with "iterator" in the above - I'm much more familiar with pointers than general iterators, although since pointers are a special case, the issues are the same.
    Last edited by robatino; 01-03-2007 at 11:18 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  2. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 06:56 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21