Re: Pointer Arithmetic & UB

Discussion in 'C Programming' started by Prathamesh Kulkarni, Dec 10, 2012.

  1. Instead, of i and j
    would *(a + 20 - 15) still have
    undefined behaviour ? Probably any C compiler
    will evaluate 20 - 15 during translation, but
    does the standard require constant expression
    to be evaluated during translation
    even if it is a sub-expression
    as in the above case ?

    Thanks ;)
    Prathamesh Kulkarni, Dec 10, 2012
    #1
    1. Advertising

  2. Prathamesh Kulkarni

    James Kuyper Guest

    On 12/10/2012 03:04 PM, Prathamesh Kulkarni wrote:
    > Instead, of i and j
    > would *(a + 20 - 15) still have
    > undefined behaviour ? Probably any C compiler
    > will evaluate 20 - 15 during translation, but
    > does the standard require constant expression
    > to be evaluated during translation
    > even if it is a sub-expression
    > as in the above case ?


    No, it does not. It would be an unusual compiler that failed to do so,
    but it is not required.
    James Kuyper, Dec 10, 2012
    #2
    1. Advertising

  3. On Monday, 10 December 2012 15:29:18 UTC-5, James Kuyper wrote:
    > On 12/10/2012 03:04 PM, Prathamesh Kulkarni wrote:
    >
    > > Instead, of i and j

    >
    > > would *(a + 20 - 15) still have

    >
    > > undefined behaviour ? Probably any C compiler

    >
    > > will evaluate 20 - 15 during translation, but

    >
    > > does the standard require constant expression

    >
    > > to be evaluated during translation

    >
    > > even if it is a sub-expression

    >
    > > as in the above case ?

    >
    >
    >
    > No, it does not. It would be an unusual compiler that failed to do so,
    >
    > but it is not required.


    Yes, I got that, that's why I deleted my post immediately
    after posting. How were you able to see it ?
    How
    Prathamesh Kulkarni, Dec 10, 2012
    #3
  4. On Monday, 10 December 2012 15:44:37 UTC-5, Prathamesh Kulkarni wrote:
    > On Monday, 10 December 2012 15:29:18 UTC-5, James Kuyper wrote:
    >
    > > On 12/10/2012 03:04 PM, Prathamesh Kulkarni wrote:

    >
    > >

    >
    > > > Instead, of i and j

    >
    > >

    >
    > > > would *(a + 20 - 15) still have

    >
    > >

    >
    > > > undefined behaviour ? Probably any C compiler

    >
    > >

    >
    > > > will evaluate 20 - 15 during translation, but

    >
    > >

    >
    > > > does the standard require constant expression

    >
    > >

    >
    > > > to be evaluated during translation

    >
    > >

    >
    > > > even if it is a sub-expression

    >
    > >

    >
    > > > as in the above case ?

    >
    > >

    >
    > >

    >
    > >

    >
    > > No, it does not. It would be an unusual compiler that failed to do so,

    >
    > >

    >
    > > but it is not required.

    >
    >
    >
    > Yes, I got that, that's why I deleted my post immediately
    >
    > after posting. How were you able to see it ?
    >
    > How


    It repeated the word "How" ;)
    Is there some problem with the google interface ?
    Prathamesh Kulkarni, Dec 10, 2012
    #4
  5. James Kuyper <> wrote:
    > On 12/10/2012 03:04 PM, Prathamesh Kulkarni wrote:
    >> Instead, of i and j
    >> would *(a + 20 - 15) still have
    >> undefined behaviour ? Probably any C compiler
    >> will evaluate 20 - 15 during translation, but
    >> does the standard require constant expression
    >> to be evaluated during translation
    >> even if it is a sub-expression
    >> as in the above case ?


    > No, it does not. It would be an unusual compiler that
    > failed to do so, but it is not required.


    Well, constant expressions in context where constants
    are needed are required to be evaluated at compile time,
    but this (whole) expression isn't constant.

    In Fortran 66 and 77 you couldn't

    DIMENSION X(10+10)

    but in C you always could. (Not counting VLAs)

    It used to be suggested that one write Fortran constants with
    the appropriate type, such as 0.0 instead of 0, to avoid
    run-time conversion. One hoped that compilers would figure it
    out, but you were never quite sure enough.

    -- glen
    >
    glen herrmannsfeldt, Dec 10, 2012
    #5
  6. Prathamesh Kulkarni

    James Kuyper Guest

    On 12/10/2012 03:47 PM, Prathamesh Kulkarni wrote:
    ....
    >> Yes, I got that, that's why I deleted my post immediately


    Forged cancellation notices used to be common; as a result, most news
    servers now ignore cancellation requests that come from other news
    servers. Your request presumably deleted the message from Google's
    archive, which is one of the largest and most widely used on the planet
    - but it probably didn't delete many of the thousands of other copies
    stored stored in various places around the world.

    >> after posting. How were you able to see it ?
    >>
    >> How

    >
    > It repeated the word "How" ;)
    > Is there some problem with the google interface ?


    That's sort of like asking if the sea is wet. I recommend getting better
    news server (I can strongly recommend eternalseptember.org, which is
    free) and a monitoring it using decent newsreader (my favorite is
    Mozilla thunderbird, but many people prefer other news readers).
    James Kuyper, Dec 10, 2012
    #6
  7. On Monday, 10 December 2012 16:02:39 UTC-5, James Kuyper wrote:
    > On 12/10/2012 03:47 PM, Prathamesh Kulkarni wrote:
    >
    > ...
    >
    > >> Yes, I got that, that's why I deleted my post immediately

    >
    >
    >
    > Forged cancellation notices used to be common; as a result, most news
    >
    > servers now ignore cancellation requests that come from other news
    >
    > servers. Your request presumably deleted the message from Google's
    >
    > archive, which is one of the largest and most widely used on the planet
    >
    > - but it probably didn't delete many of the thousands of other copies
    >
    > stored stored in various places around the world.
    >
    >
    >
    > >> after posting. How were you able to see it ?

    >
    > >>

    >
    > >> How

    >
    > >

    >
    > > It repeated the word "How" ;)

    >
    > > Is there some problem with the google interface ?

    >
    >
    >
    > That's sort of like asking if the sea is wet. I recommend getting better
    >
    > news server (I can strongly recommend eternalseptember.org, which is
    >
    > free) and a monitoring it using decent newsreader (my favorite is
    >
    > Mozilla thunderbird, but many people prefer other news readers).


    Thanks, I will give a try to eternalseptember.org,
    Prathamesh Kulkarni, Dec 10, 2012
    #7
  8. Prathamesh Kulkarni

    Sjouke Burry Guest

    Prathamesh Kulkarni <> wrote in news:232fe1b0-
    :

    > On Monday, 10 December 2012 15:29:18 UTC-5, James Kuyper wrote:
    >> On 12/10/2012 03:04 PM, Prathamesh Kulkarni wrote:
    >>
    >> > Instead, of i and j

    >>
    >> > would *(a + 20 - 15) still have

    >>
    >> > undefined behaviour ? Probably any C compiler

    >>
    >> > will evaluate 20 - 15 during translation, but

    >>
    >> > does the standard require constant expression

    >>
    >> > to be evaluated during translation

    >>
    >> > even if it is a sub-expression

    >>
    >> > as in the above case ?

    >>
    >>
    >>
    >> No, it does not. It would be an unusual compiler that failed to do so,
    >>
    >> but it is not required.

    >
    > Yes, I got that, that's why I deleted my post immediately
    > after posting. How were you able to see it ?
    > How
    >


    By looking carefully.....
    Sjouke Burry, Dec 10, 2012
    #8
  9. Prathamesh Kulkarni

    Greg Martin Guest

    On 12-12-10 01:02 PM, James Kuyper wrote:
    > On 12/10/2012 03:47 PM, Prathamesh Kulkarni wrote:
    > ...
    >>> Yes, I got that, that's why I deleted my post immediately

    >
    > Forged cancellation notices used to be common; as a result, most news
    > servers now ignore cancellation requests that come from other news
    > servers. Your request presumably deleted the message from Google's
    > archive, which is one of the largest and most widely used on the planet
    > - but it probably didn't delete many of the thousands of other copies
    > stored stored in various places around the world.
    >
    >>> after posting. How were you able to see it ?
    >>>
    >>> How

    >>
    >> It repeated the word "How" ;)
    >> Is there some problem with the google interface ?

    >
    > That's sort of like asking if the sea is wet. I recommend getting better
    > news server (I can strongly recommend eternalseptember.org, which is
    > free) and a monitoring it using decent newsreader (my favorite is
    > Mozilla thunderbird, but many people prefer other news readers).
    >


    I think that should be eternal-september.org.
    Greg Martin, Dec 10, 2012
    #9
  10. In article <>,
    Prathamesh Kulkarni <> wrote:
    >
    >Yes, I got that, that's why I deleted my post immediately
    >after posting. How were you able to see it ?
    >How


    News servers are not required to honor cancels, and many do
    not. Also, cancels can take time to propagate through the
    network, so previous poster could have seen your article and
    responded to it before it was deleted.

    --
    -Ed Falk,
    http://thespamdiaries.blogspot.com/
    Edward A. Falk, Dec 11, 2012
    #10
  11. Prathamesh Kulkarni

    Ken Brody Guest

    On 12/10/2012 3:29 PM, James Kuyper wrote:
    > On 12/10/2012 03:04 PM, Prathamesh Kulkarni wrote:
    >> Instead, of i and j
    >> would *(a + 20 - 15) still have
    >> undefined behaviour ? Probably any C compiler
    >> will evaluate 20 - 15 during translation, but
    >> does the standard require constant expression
    >> to be evaluated during translation
    >> even if it is a sub-expression
    >> as in the above case ?

    >
    > No, it does not. It would be an unusual compiler that failed to do so,
    > but it is not required.


    When I went to college, there was a FORTRAN compiler aimed specifically at
    teaching FORTRAN programming. Since it was expected that there would be
    more time spent compiling (and recompiling, after fixing errors) the program
    than in running it (the resulting program would in all likelihood be run
    only once), there was zero optimization done on the resulting code. This
    made the compiler itself "lean and mean", allowing it to finish compiling
    (or getting to the errors) faster. It didn't make sense to spend X amount
    of time optimizing a program, only to save less than X time once the program
    actually ran the one time it would be run.

    I could see such an implementation of C being developed.
    Ken Brody, Dec 12, 2012
    #11
  12. Prathamesh Kulkarni

    Ken Brody Guest

    On 12/10/2012 4:08 PM, Prathamesh Kulkarni wrote:
    > On Monday, 10 December 2012 16:02:39 UTC-5, James Kuyper wrote:
    >> On 12/10/2012 03:47 PM, Prathamesh Kulkarni wrote:

    [...]
    >>> Is there some problem with the google interface ?

    >>
    >>
    >>
    >> That's sort of like asking if the sea is wet. I recommend getting better
    >> news server (I can strongly recommend eternalseptember.org, which is
    >> free) and a monitoring it using decent newsreader (my favorite is
    >> Mozilla thunderbird, but many people prefer other news readers).

    >
    > Thanks, I will give a try to eternalseptember.org,


    There's a hyphen there:

    eternal-september.org
    Ken Brody, Dec 12, 2012
    #12
  13. Ken Brody <> wrote:

    (snip on expression evaluation order)

    > When I went to college, there was a FORTRAN compiler aimed specifically at
    > teaching FORTRAN programming. Since it was expected that there would be
    > more time spent compiling (and recompiling, after fixing errors) the program
    > than in running it (the resulting program would in all likelihood be run
    > only once), there was zero optimization done on the resulting code. This
    > made the compiler itself "lean and mean", allowing it to finish compiling
    > (or getting to the errors) faster. It didn't make sense to spend X amount
    > of time optimizing a program, only to save less than X time once the program
    > actually ran the one time it would be run.


    Maybe about that time, there was a story about benchmarks for Fortran
    compilers. There was one that evaluated many complicated expressions,
    mostly using statement functions.

    This benchmark was then given to the OS/360 Fortran H compiler with
    optimization level of 2, where it compiled very slowly and ran in
    pretty much no time at all.

    It turns out that the compiler expands statement functions inline
    (seems to have been rare at the time), and then evaluates constant
    expressions. The compiler did the whole calculation down to one
    number to print out!

    > I could see such an implementation of C being developed.


    The favorite Fortran compilers I knew of for student programs were
    WATFOR and WATFIV. Both are in-core compilers. They don't write out
    an object program, but instead generate instructions in memory, ready
    to run.

    I believe that there is a WAT-C, but don't know anything about it.

    The WATCOM compilers don't seem to be in-core compilers.

    I do remember MS had a Quick-C which might have been (and still be)
    an in-core compiler.

    -- glen
    glen herrmannsfeldt, Dec 12, 2012
    #13
  14. Robert Wessel <> wrote:

    (snip, I wrote)

    >>I do remember MS had a Quick-C which might have been (and still be)
    >>an in-core compiler.


    > QuickC was MS's IDE, a precursor to Visual C++ and Visual Studio.
    > Under the hood it used the contemporary MSC compilers.


    I had one, as it came with the MSC compiler, but don't think I
    ever used it.

    Still, I always thought it had a different compiler inside.
    Maybe not a lot different.

    -- glen
    glen herrmannsfeldt, Dec 13, 2012
    #14
  15. On Wednesday, December 12, 2012 7:24:05 PM UTC, Ken Brody wrote:
    > On 12/10/2012 3:29 PM, James Kuyper wrote:
    >


    > When I went to college, there was a FORTRAN compiler aimed specifically at
    > teaching FORTRAN programming. Since it was expected that there would be
    > more time spent compiling (and recompiling, after fixing errors) the program
    > than in running it (the resulting program would in all likelihood be run
    > only once), there was zero optimization done on the resulting code. This
    > made the compiler itself "lean and mean", allowing it to finish compiling
    > (or getting to the errors) faster. It didn't make sense to spend X amount
    > of time optimizing a program, only to save less than X time once the program
    > actually ran the one time it would be run.
    >

    Sometimes when you're developing code, you need to know that it's correct.
    Other times you need to know that it's correct and fast enough.

    For instance if you are developing a video game, then typically you'll
    write a bit of code, then run the game to see what effect your changes have
    on the player's experience. So normally the program will have to run at the
    same speed the end-user will be receiving.
    Malcolm McLean, Dec 17, 2012
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. a

    pointer arithmetic

    a, Sep 11, 2003, in forum: C++
    Replies:
    6
    Views:
    507
  2. Marc Schellens

    Iterator/pointer arithmetic

    Marc Schellens, Dec 5, 2003, in forum: C++
    Replies:
    15
    Views:
    833
    tom_usenet
    Dec 8, 2003
  3. dan

    Pointer arithmetic

    dan, Jan 6, 2004, in forum: C++
    Replies:
    1
    Views:
    348
    Jeff Schwab
    Jan 6, 2004
  4. ceo
    Replies:
    8
    Views:
    339
    Pete Becker
    Mar 10, 2005
  5. joshc
    Replies:
    5
    Views:
    537
    Keith Thompson
    Mar 31, 2005
Loading...

Share This Page