View Single Post
Old 23 November 2020, 18:21   #1
Ernst Blofeld
<optimized out>

Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 304
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?
Ernst Blofeld is offline  
 
Page generated in 0.04809 seconds with 11 queries