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. will this be compiler(M$ cl) bug or Memory leakage?

will this be compiler(M$ cl) bug or Memory leakage?

Scheduled Pinned Locked Moved C / C++ / MFC
helpperformancequestion
3 Posts 2 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.
  • D Offline
    D Offline
    DengJW
    wrote on last edited by
    #1

    #include
    #define unsafe(i) \
    ( (i) >= 0 ? (i) : -(i) )

    inline
    int safe(int i)
    {
    return i >= 0 ? i : -i;
    }
    int f() {cout << "Called f()!" << endl; return 0;};
    void userCode(int x)
    {
    int ans;
    ans = unsafe(x++); // Error! x is incremented twice
    cout << "[1] "<< x << endl;
    // ans = (f()) >= 0 ? (f()) : -(f());
    ans = unsafe(f()); // Danger! f() is called twice
    cout << "[2] "<< x << endl;

    ans = safe(x++); // Correct! x is incremented once
    cout << "[3] "<< x << endl;
    ans = safe(f()); // Correct! f() is called once
    cout << "[4] "<< x << endl;
    }

    void main(void)
    {
    userCode(100);
    }

    I was doing a test code for inline functions comparison to macros. The original code was shown as above, things seemed normal as the result showed two times of f() calls. The result looked like:

    [1] 102
    Called f();
    Called f();
    [2] 102
    [3] 103
    Called f();
    [4] 103

    However, after I tried to call ans = (f()) >= 0 ? (f()) : -(f()); instead of ans = unsafe(f());, I got a result of calling f() FOUR times instead of TWO times as supposed. In this tread, I failed to attach a image of the result, which looked like:

    [1] 102
    Called f();
    Called f();
    Called f();
    Called f();
    [2] 102
    [3] 103
    Called f();
    [4] 103

    I ran the program twice both of times I got calling f() FOUR times, nevertheless, I could repeat the result after I re-compiled the program again with the original code. Could this be a compiler bug? DJ

    M 1 Reply Last reply
    0
    • D DengJW

      #include
      #define unsafe(i) \
      ( (i) >= 0 ? (i) : -(i) )

      inline
      int safe(int i)
      {
      return i >= 0 ? i : -i;
      }
      int f() {cout << "Called f()!" << endl; return 0;};
      void userCode(int x)
      {
      int ans;
      ans = unsafe(x++); // Error! x is incremented twice
      cout << "[1] "<< x << endl;
      // ans = (f()) >= 0 ? (f()) : -(f());
      ans = unsafe(f()); // Danger! f() is called twice
      cout << "[2] "<< x << endl;

      ans = safe(x++); // Correct! x is incremented once
      cout << "[3] "<< x << endl;
      ans = safe(f()); // Correct! f() is called once
      cout << "[4] "<< x << endl;
      }

      void main(void)
      {
      userCode(100);
      }

      I was doing a test code for inline functions comparison to macros. The original code was shown as above, things seemed normal as the result showed two times of f() calls. The result looked like:

      [1] 102
      Called f();
      Called f();
      [2] 102
      [3] 103
      Called f();
      [4] 103

      However, after I tried to call ans = (f()) >= 0 ? (f()) : -(f()); instead of ans = unsafe(f());, I got a result of calling f() FOUR times instead of TWO times as supposed. In this tread, I failed to attach a image of the result, which looked like:

      [1] 102
      Called f();
      Called f();
      Called f();
      Called f();
      [2] 102
      [3] 103
      Called f();
      [4] 103

      I ran the program twice both of times I got calling f() FOUR times, nevertheless, I could repeat the result after I re-compiled the program again with the original code. Could this be a compiler bug? DJ

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      What compiler are you using? I can't repro the problem on VC 6 SP 5. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Kosh reminded me of some of the prima-donna programmers I've worked with. Knew everything but when you asked them a question; never gave you a straight answer.   -- Michael P. Butler in the Lounge

      D 1 Reply Last reply
      0
      • M Michael Dunn

        What compiler are you using? I can't repro the problem on VC 6 SP 5. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Kosh reminded me of some of the prima-donna programmers I've worked with. Knew everything but when you asked them a question; never gave you a straight answer.   -- Michael P. Butler in the Lounge

        D Offline
        D Offline
        DengJW
        wrote on last edited by
        #3

        Microsoft Compiler Ver 12.00.8168 from VC++ 6. I could not reproduce the problem either. So I guess the chance would be high that I accidently uncommented both ans = (f()) >= 0 ? (f()) : -(f()); and ans = unsafe(f());. So far, I could not think out other reasons. DJ

        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