Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. v[f(x)]+=j;

v[f(x)]+=j;

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmsdata-structuresperformancequestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pupillo0
    wrote on last edited by
    #1

    Hi, v is an array; in v[f(x)]+=j; will f(x) be evaluated once or twice (one for reading and one for writing)? I guess once (at least due to optimization) but I'd like to know if C language ensures that it will be once and not twice. Thanks Tom

    S D J 3 Replies Last reply
    0
    • P pupillo0

      Hi, v is an array; in v[f(x)]+=j; will f(x) be evaluated once or twice (one for reading and one for writing)? I guess once (at least due to optimization) but I'd like to know if C language ensures that it will be once and not twice. Thanks Tom

      S Offline
      S Offline
      Stefan_Lang
      wrote on last edited by
      #2

      A good question, and one that yould be tricky to answer under different circumstances*. But for your purposes it's safe to answer that any of the compound assignment statements evaluate the adress of the modified value just once! *: it would be tricky indeed if f() could modify v! ;)

      GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

      1 Reply Last reply
      0
      • P pupillo0

        Hi, v is an array; in v[f(x)]+=j; will f(x) be evaluated once or twice (one for reading and one for writing)? I guess once (at least due to optimization) but I'd like to know if C language ensures that it will be once and not twice. Thanks Tom

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        I'm inclined to think that it took you longer to type your question than it would have to just compile that piece of code and find out.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        P 1 Reply Last reply
        0
        • D David Crow

          I'm inclined to think that it took you longer to type your question than it would have to just compile that piece of code and find out.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          P Offline
          P Offline
          pupillo0
          wrote on last edited by
          #4

          I must write portable code (x86, ARM etc.....) and I was interested in understanding what C rules exactly say.

          D 1 Reply Last reply
          0
          • P pupillo0

            I must write portable code (x86, ARM etc.....) and I was interested in understanding what C rules exactly say.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            It's going to depend on the particular compiler's implementation (of sequence points).

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            1 Reply Last reply
            0
            • P pupillo0

              Hi, v is an array; in v[f(x)]+=j; will f(x) be evaluated once or twice (one for reading and one for writing)? I guess once (at least due to optimization) but I'd like to know if C language ensures that it will be once and not twice. Thanks Tom

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Per the following it suggests that there is an implicit requirement in C that it must be so. http://en.wikipedia.org/wiki/Sequence_point#cite_note-6[^] That said of course the simple way to insure it is to rewrite the code to insure it.

              P 1 Reply Last reply
              0
              • J jschell

                Per the following it suggests that there is an implicit requirement in C that it must be so. http://en.wikipedia.org/wiki/Sequence_point#cite_note-6[^] That said of course the simple way to insure it is to rewrite the code to insure it.

                P Offline
                P Offline
                pupillo0
                wrote on last edited by
                #7

                Perhaps, I found out the right answer, form the C99 standard, about compound ass.: Semantics 3 A compound assignment of the form E1 op= E2 differs from the simple assignment expression E1 = E1 op (E2) only in that the lvalue E1 is evaluated only once. So E1 is evaluated ONCE

                J 1 Reply Last reply
                0
                • P pupillo0

                  Perhaps, I found out the right answer, form the C99 standard, about compound ass.: Semantics 3 A compound assignment of the form E1 op= E2 differs from the simple assignment expression E1 = E1 op (E2) only in that the lvalue E1 is evaluated only once. So E1 is evaluated ONCE

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #8

                  No that isn't what that means. E1 appears on both side of the equals. It is referring to whether E1 is evaluated once for the lvalue side and once for the rvalue side. And it says no.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups