Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 323789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:03:48+00:00 2026-05-12T09:03:48+00:00

Here is a very simplified program which reproduces the issue I faced in the

  • 0

Here is a very simplified program which reproduces the issue I faced in the real application:

#include "stdlib.h"
typedef unsigned short int shft;
struct xptr  {
    int layer;
    void *  addr;

    xptr() : layer(0), addr(NULL) {}
    xptr(int _layer, void *_addr) : layer(_layer), addr(_addr) {}

    xptr(const xptr& x) { layer = x.layer; addr = x.addr; }

/* Uncomment this to remove the bug */
/*   const xptr& operator= (const xptr& rhs) 
    {
        if (this != &rhs) {
            this->addr = rhs.addr;
            this->layer = rhs.layer;
        }
        return *this;
    }*/
};


struct n_dsc {
    xptr    pdsc;      
    xptr    ldsc;      
    xptr    rdsc;      
};


void dm_string_value_traverse(xptr node)
{    
    xptr p = node;    
    while (p.addr != 0)
    {        
        p = ((n_dsc*)p.addr)->rdsc;    
    }
}

int main()
{
    n_dsc n1, n2;

    n1.rdsc = xptr(0, &n2);
    xptr n = xptr(0, &n1);

    dm_string_value_traverse(n);
}

I absolutely could not understand why CL 2008 (with /Og /Zi /W4) generates the following assembly for the “dm_string_value_traverse” function:

    while (p.addr != 0)
004ABAC5  mov         eax,dword ptr [esp+10h] 
004ABAC9  add         esp,0Ch 
004ABACC  test        eax,eax 
004ABACE  je          dm_string_value_traverse+5Fh (4ABADFh) 
    {
        p = ((n_dsc*)p.addr)->rdsc;
004ABAD0  mov         ecx,dword ptr [eax+1Ch] 
004ABAD3  mov         dword ptr [esp],ecx 
004ABAD6  mov         eax,dword ptr [eax+20h] 
004ABAD9  mov         dword ptr [esp+4],eax 
004ABADD  jmp         dm_string_value_traverse+50h (4ABAD0h) ;NOTE THIS JMP TO THE ASSIGNMENT
    }

}

Note:

  1. Condition (p.addr != 0) is checked only once! See 004ABADD jumps unconditionally to the 004ABAD0 (assignment).
  2. Without /Og compiler generates right code.
  3. If you uncomment copy constructor compiler will generate good code also.

Is it possible to understand why this is going on? Is there a way to workaround this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:03:48+00:00Added an answer on May 12, 2026 at 9:03 am

    This looks like an overzealous optimizer. I can confirm the behavior you describe with Visual Studio 2008 SP1, /Og, and /Fa for assembly output. This wouldn’t be the first time for VC: try google visual c++ “/Og” site:support.microsoft.com.

    One workaround is to iterate with an xptr pointer, instead of an xptr value. This also has the beneficial side-effect of reducing the number of bytes copied on each iteration from 8 (xptr value) to 4 (xptr pointer).

    void dm_string_value_traverse(xptr node)
    {    
        xptr *p = &node;    
        while (p->addr != 0)
        {        
            p = &(((n_dsc*)(p->addr))->rdsc);    
        }
    }
    

    The resulting assembly code, with /Og, now looks like this. I can’t map the assembly exactly to the source code, as the comparison (p->addr != 0) now happens in two places. It is clear, however, that the loop now includes a test for its end condition.

    ; prolog
          push  ebp
          mov   ebp, esp
    ; while (p->addr != 0)
          mov   eax, DWORD PTR _node$[ebp+4]
          test  eax, eax
          je    SHORT $LN1@dm_string_
          npad  6
    ; p = &(((n_dsc*)p->addr)->rdsc);
    $LL2@dm_string_:
          mov   eax, DWORD PTR [eax+20]
          test  eax, eax
          jne   SHORT $LL2@dm_string_
    $LN1@dm_string_:
    ; epilog
          pop   ebp
          ret   0
    

    Given how intractable this class of bug is, there may be similar issues lurking invisible in the code. For the portion of the code that deals with xptr’s and n_dsc’s, you might want to consider going without /Og, or unit-testing it all around.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 174k
  • Answers 174k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer After a million different tries I finally ran into this… May 12, 2026 at 2:49 pm
  • Editorial Team
    Editorial Team added an answer There could be several different stylesheets in your Wordpress installation… May 12, 2026 at 2:49 pm
  • Editorial Team
    Editorial Team added an answer P/Invoking GetSystemInfo is trivial from .Net and is much lighter… May 12, 2026 at 2:49 pm

Related Questions

I am somewhat new to Ruby and although I find it to be a
I am writing a java program that needs a file open dialog. The file
I'm trying to use TestDriven.Net not only to test my code, but to call
Here's my proposed (very simplified to illustrate the problem space) design for a C#

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.