开发者

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

开发者 https://www.devze.com 2022-12-09 03:36 出处:网络
I\'ve got a weird issue that almost seems like a Visual Studio 2008 issue.I have a C struct definition as follows:

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 {
    s开发者_如何学Chort 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.


There's something about your situation described by Microsoft: FIX: Wrong Type Definition Appears in Visual Studio .NET Debugger

WORKAROUND: Microsoft strongly recommends that you use unique type definitions. By using unique type definitions, you can avoid any confusion about the true value of a data structure. However, if you cannot use unique type definitions, you can also avoid the problem by using namespaces, as in the following sample code:

namespace MyNamespace {
struct IDENTICALSTRUCT {
...;
};

using namespace MyNamespace;

The debugger can then resolve the type definitions correctly.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消