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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. about *p++ [modified]

about *p++ [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
3 Posts 3 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.
  • H Offline
    H Offline
    HOW WHAT
    wrote on last edited by
    #1

    p1++ = map[((*p++ << 5) & 0x3) | ((*p >> 3) & 0x5)]; <-error, "p++" inoperative
    p1++ = map[((*p++ << 7) & 0x5); <- ok, "p++"

    but vc compiler do it like this

    p1++ = map[((*p << 5) & 0x3) | ((*p >> 3) & 0x5)];
    p++;
    p1++ = map[((*p << 7) & 0x5);
    p++;

    why not

    value = map[((*p << 5) & 0x3);
    p++;
    value |= ((*p >> 3) & 0x5)];
    p1++ = value;
    p1++ = map[((*p << 7) & 0x5);
    p++;

    how to fix it only use "++"

    modified on Sunday, March 7, 2010 6:33 AM

    L M 2 Replies Last reply
    0
    • H HOW WHAT

      p1++ = map[((*p++ << 5) & 0x3) | ((*p >> 3) & 0x5)]; <-error, "p++" inoperative
      p1++ = map[((*p++ << 7) & 0x5); <- ok, "p++"

      but vc compiler do it like this

      p1++ = map[((*p << 5) & 0x3) | ((*p >> 3) & 0x5)];
      p++;
      p1++ = map[((*p << 7) & 0x5);
      p++;

      why not

      value = map[((*p << 5) & 0x3);
      p++;
      value |= ((*p >> 3) & 0x5)];
      p1++ = value;
      p1++ = map[((*p << 7) & 0x5);
      p++;

      how to fix it only use "++"

      modified on Sunday, March 7, 2010 6:33 AM

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If you use a pointer (or indeed a value) in an expression, and the expression modifies that value (as you do above with p++) then the compiler will not guarantee that the new and old values are used in the order that you expect. This is due to compiler optimisation and is spelled out in the C++ language specification. In the line

      p1++ = map[((*p++ << 5) & 0x3) | ((*p >> 3) & 0x5)];

      if p contains 0x100 before this line is executed, then it will contain 0x101 afterwards. However there is no guarantee that it will contain 0x101 in the right half of the OR expression. You should rewrite your expressions to ensure no side effects.

      txtspeak is the realm of 9 year old children, not developers. Christian Graus

      1 Reply Last reply
      0
      • H HOW WHAT

        p1++ = map[((*p++ << 5) & 0x3) | ((*p >> 3) & 0x5)]; <-error, "p++" inoperative
        p1++ = map[((*p++ << 7) & 0x5); <- ok, "p++"

        but vc compiler do it like this

        p1++ = map[((*p << 5) & 0x3) | ((*p >> 3) & 0x5)];
        p++;
        p1++ = map[((*p << 7) & 0x5);
        p++;

        why not

        value = map[((*p << 5) & 0x3);
        p++;
        value |= ((*p >> 3) & 0x5)];
        p1++ = value;
        p1++ = map[((*p << 7) & 0x5);
        p++;

        how to fix it only use "++"

        modified on Sunday, March 7, 2010 6:33 AM

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        I'd code-review you back to kindergarden if I was presented code like that.

        Watched code never compiles.

        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