English Amiga Board

English Amiga Board (http://eab.abime.net/index.php)
-   Coders. C/C++ (http://eab.abime.net/forumdisplay.php?f=118)
-   -   Structures via pointers giving different results to direct dot access (http://eab.abime.net/showthread.php?t=104782)

Ernst Blofeld 23 November 2020 18:21

Structures via pointers giving different results to direct dot access
 
i can't figure this out.

This code:

Code:

void __Eurofighter_construct(void * _eurofighter /* position, orientation */) {
    KPrintF("In __Eurofighter_construct.");

    KPrintF("// these things work:");

    BSPModelData * b = &eurofighterBSPModelData;
    KPrintF("  b->modelData.numVertices = %ld", b->modelData.numVertices);

    ModelData * m = (ModelData *) b;
    KPrintF("  m->numVertices = %ld", m->numVertices);

    KPrintF("// but this doesn't:");

    KPrintF("  eurofighterBSPModelData.modelData.numVertices = %d", eurofighterBSPModelData.modelData.numVertices);

    KPrintF("Huh.");

    BSPModel * eurofighterModel = new_BSPModel(&eurofighterBSPModelData);

    __EntityWithModel_construct((EntityWithModel *) _eurofighter, /* position, orientation */ (Model *) eurofighterModel);
}

Results in this output:

Code:

In __Eurofighter_construct.
// these things work:
  b->modelData.numVertices = 52
  m->numVertices = 52
// but this doesn't:
  eurofighterBSPModelData.modelData.numVertices = 0
Huh.

I expect numVertices should be 52 in all 3 cases, it's the same data... I don't understand why the last one comes as 0.

The structure ModelData is defined like this:

Code:

typedef struct modelData {
    UWORD numVertices;
    Point3D * vertices;
} ModelData;

and BSPModelData is this:

Code:

typedef struct bspModelData {
    ModelData modelData;
    Section * bspTreeRoot;
} BSPModelData;

And the data being printed is:

Code:

static BSPModelData eurofighterBSPModelData = {
    .modelData = {
        .numVertices = EUROFIGHTER_NUM_VERTICES,
        .vertices = vertices
    },
    .bspTreeRoot = (Section *) &eurofighterSection
};

Where EUROFIGHTER_NUM_VERTICES is #defined to be 52.

Any ideas?

Samurai_Crow 23 November 2020 18:25

Using a dot operator with a pointer requires you dereference using a star.

thomas 23 November 2020 18:29

KPrintF(" eurofighterBSPModelData.modelData.numVertices = %d"

This should read %ld, like in the other places.

The code is correct, the debugging isn't.

Ernst Blofeld 23 November 2020 18:29

Quote:

Originally Posted by Samurai_Crow (Post 1442526)
Using a dot operator with a pointer requires you dereference using a star.

There are 14 stars in my post. Do I not have enough?

thomas 23 November 2020 18:33

In case you missed my post: your code is doing what you expect. Your debug output is wrong.

Ernst Blofeld 23 November 2020 18:33

Quote:

Originally Posted by thomas (Post 1442527)
KPrintF(" eurofighterBSPModelData.modelData.numVertices = %d"

This should read %ld, like in the other places.

The code is correct, the debugging isn't.

Well, spotted. Fixing that changes the output to:

Code:

// but this doesn't:
  eurofighterBSPModelData.modelData.numVertices = 52
Huh.

So my problem must be elsewhere.

Thanks!

Ernst Blofeld 23 November 2020 19:30

Found it.

I had:

Code:

    Model * model = (Model *) model;
instead of

Code:

    Model * model = (Model *) _model;
Not sure how this compiles, but

Code:

    Model * wibble = (Model *) wibble;
also compiles, and wibble definitely doesn't exist anywhere else in my code.

I think the best thing for me to do is to rework my variable naming to make this less likely to happen again.

Edit:

Code:

    Model * wobble = wobble;
also compiles, so I assume there's part of the language standard I don't understand.

Samurai_Crow 23 November 2020 19:48

C isn't picky. It just assumes you are right even if you aren't.

thomas 23 November 2020 21:34

Quote:

Originally Posted by Ernst Blofeld (Post 1442542)
Not sure how this compiles, but

Code:

    Model * wibble = (Model *) wibble;
also compiles, and wibble definitely doesn't exist anywhere else in my code.

Why do you say it does not exist? You make it exist kust by that line.

The line is just a short form of

Code:

Model *wibble;
wibble = (Model *)wibble;


What you really should do is to avoid all that casting. Use proper types for your variables.

For example in your first post instead of

ModelData *m = (ModelData *) b;

you should use

ModelData *m = &b->modelData;

This way the compiller can help you. If you use casting you tell the compiler that you know better than him. But obviously you don't.

Ernst Blofeld 23 November 2020 22:28

Quote:

Originally Posted by thomas (Post 1442574)
Why do you say it does not exist? You make it exist kust by that line.

The line is just a short form of

Code:

Model *wibble;
wibble = (Model *)wibble;


What you really should do is to avoid all that casting. Use proper types for your variables.

For example in your first post instead of

ModelData *m = (ModelData *) b;

you should use

ModelData *m = &b->modelData;

This way the compiller can help you. If you use casting you tell the compiler that you know better than him. But obviously you don't.

Sure, but that doesn't work the other way, getting from a ModelData * to a BSPModelData *, so I'd end up with two approaches in the same code base.

defor 02 December 2020 17:18

Quote:

Originally Posted by Ernst Blofeld (Post 1442590)
Sure, but that doesn't work the other way, getting from a ModelData * to a BSPModelData *, so I'd end up with two approaches in the same code base.

Using C++ isn't an option for you?

Ernst Blofeld 02 December 2020 17:30

Quote:

Originally Posted by defor (Post 1444030)
Using C++ isn't an option for you?

I could, and for the relatively simple stuff I'm doing it would probably result in almost identical results when compiled, but I, personally, just don't want to.


All times are GMT +2. The time now is 18:59.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.

Page generated in 0.04450 seconds with 11 queries