English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 23 November 2020, 19:21   #1
Ernst Blofeld
<optimized out>

Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 275
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 online now  
Old 23 November 2020, 19:25   #2
Samurai_Crow
Total Chaos forever!

Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 46
Posts: 1,624
Send a message via Yahoo to Samurai_Crow
Using a dot operator with a pointer requires you dereference using a star.
Samurai_Crow is offline  
Old 23 November 2020, 19:29   #3
thomas
Registered User
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,191
KPrintF(" eurofighterBSPModelData.modelData.numVertices = %d"

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

The code is correct, the debugging isn't.
thomas is offline  
Old 23 November 2020, 19:29   #4
Ernst Blofeld
<optimized out>

Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 275
Quote:
Originally Posted by Samurai_Crow View Post
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?
Ernst Blofeld is online now  
Old 23 November 2020, 19:33   #5
thomas
Registered User
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,191
In case you missed my post: your code is doing what you expect. Your debug output is wrong.
thomas is offline  
Old 23 November 2020, 19:33   #6
Ernst Blofeld
<optimized out>

Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 275
Quote:
Originally Posted by thomas View Post
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 is online now  
Old 23 November 2020, 20:30   #7
Ernst Blofeld
<optimized out>

Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 275
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.

Last edited by Ernst Blofeld; 23 November 2020 at 20:36.
Ernst Blofeld is online now  
Old 23 November 2020, 20:48   #8
Samurai_Crow
Total Chaos forever!

Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 46
Posts: 1,624
Send a message via Yahoo to Samurai_Crow
C isn't picky. It just assumes you are right even if you aren't.
Samurai_Crow is offline  
Old 23 November 2020, 22:34   #9
thomas
Registered User
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,191
Quote:
Originally Posted by Ernst Blofeld View Post
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.
thomas is offline  
Old 23 November 2020, 23:28   #10
Ernst Blofeld
<optimized out>

Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 275
Quote:
Originally Posted by thomas View Post
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.
Ernst Blofeld is online now  
Old 02 December 2020, 18:18   #11
defor
Registered User

 
Join Date: Jun 2020
Location: Brno
Posts: 25
Quote:
Originally Posted by Ernst Blofeld View Post
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?
defor is offline  
Old 02 December 2020, 18:30   #12
Ernst Blofeld
<optimized out>

Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 275
Quote:
Originally Posted by defor View Post
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.
Ernst Blofeld is online now  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Structures offsets kamelito Coders. System 16 24 January 2021 08:26
It seem the JIT direct mode is not work in fs-uae. direct mode is important bernd roesch support.FS-UAE 27 20 September 2015 22:44
Memory Data Structures Zetr0 support.Hardware 7 30 September 2007 18:38
IFF/ILBM structures .... freddix Coders. General 7 18 September 2006 10:54
Slow speed Direct HD access Dan Andrea support.WinUAE 3 27 December 2002 15:21

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:28.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.
Page generated in 0.10435 seconds with 15 queries