Why does Visual Studio not know the correct defini

2019-09-02 18:45发布

问题:

I've got a weird issue that almost seems like a Visual Studio 2008 issue. I have a C struct definition as follows:

static struct frame {
    short typupdt;
    char callarg[1+CallSiz];
    char *unitarg;
    XTime unitage;
    XTime orgtime;
    XTime newtime;
    char oldstat[1+StatSiz];
    char newstat[1+StatSiz];
    char incdisp[1+DispSiz];
    char orgdisp[1+DispSiz];
    char clearcod[1+ClearSiz];
    char orgclear[1+ClearSiz];
    char observd[1+ObsSiz];
    char orgobs[1+ObsSiz];
    char raddesc[1+Desc1Siz];
    char incnum[INVIDLEN];
    char agency[1+AgencySiz];
    int wlins;
    int wcols;
    int skipsrch;
    struct frame *next;
} *Frame= NULL;

Which should (and seems to) create a new struct called frame and a global pointer (to this file) to an instance of that struct called Frame. That all seems to work fine in the code itself. However, when I am debugging this code and set a break point somewhere and then examine Frame in the watch window, the information it reports is completely wrong. It's like it's looking at the correct piece of memory, but its understanding of the definition is incorrect, i.e. the fields it says the struct has are not even close.

At first I thought there was sort of weird namespacing issue or something so I changed the names of both frame and Frame, but the issue still existed. Anybody have any idea what is going on? Like I said, the code seems to work, but debugging is pretty much impossible.

Edit: I updated the definition with the real definition, and here's a screenshot of what I see in the watch window:

alt text http://img156.imageshack.us/img156/6943/watchlist.jpg

That Make a lick of sense to anybody? I'm still super stumped.

回答1:

There's something about your situation described by Microsoft: http://support.microsoft.com/kb/822551

WORKAROUND: Microsoft strongly recommends that you use unique type definitions.



回答2:

The problem is that this

struct foo { /*...*/ } * bar;

defines bar to be a foo*, not a foo. Try

struct foo { /*...*/ } bar;

instead.



回答3:

Are you running a Debug build? Debugging a release build will often seem to work, but the debugger will report garbage values for variables.

If that's not it, then I'd try to verify if it is a compiler/syntax issue by splitting up the definition so you define the struct as a typedef and then define the pointer in a separate statement. (This would arguably make the code more readable/maintainable anyway - if you don't trust the code above then rewriting it in a way that you do trust is advisable)



回答4:

Try declaring the struct frame and defining a variable of that type in different statements.

struct frame {
    /* .. Various other fields, etc */
    struct frame *next;
};
static struct frame *Frame = NULL;

Maybe the static messes up Visual Studio.