简体   繁体   中英

Why does Visual Studio not know the correct definition of this struct?

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, ie 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.

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.

The problem is that this

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

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

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

instead.

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM