SizeOf and nested structures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

SizeOf and nested structures

Post by GPI »

Code: Select all

Structure t1
  l.l
  a.a
EndStructure
Structure t2
  d.d
  b.t1
EndStructure

a.t2
Debug SizeOf(a)
Debug SizeOf(a\b)
Debug SizeOf(a\b\a) ; <- Syntax error?!

Debug SizeOf(t2)
Debug SizeOf(t2\b)
Debug SizeOf(t2\b\a); <- dito
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: SizeOf and nested structures

Post by STARGÅTE »

Would you call it a bug? I would say it is a missing feature.
From the documentation it is not clear, if SizeOf also allows nested structure calls insteat of just the field of the present structure.

A similar "missing feature" is the OffsetOf() of nested structures.
Here you need an addition:

Code: Select all

Debug OffsetOf(t2\b) + OffsetOf(t1\a)   ; Debug OffsetOf(t2\b\a) 
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: SizeOf and nested structures

Post by GPI »

the problem is, i used it at the moment to identify a structure dynamicaly.

Code: Select all

Structure ab
  StructureUnion;structure-union is the trick here 
    _type.b[1]
    somedata.l
  EndStructureUnion
EndStructure

Structure bc
  StructureUnion
    _type.b[2]
    elsedata.l
  EndStructureUnion
EndStructure

Procedure ab_doit(*a.ab)
  Debug "AB-Variant"
EndProcedure

Procedure bc_doit(*b.bc)
  Debug "BC-Variant"
EndProcedure

Macro GetType(x): SizeOf(x\_type) :EndMacro

Macro doit(x)
  CompilerSelect GetType(x)
    CompilerCase gettype(ab)
      ab_doit(x)
    CompilerCase gettype(bc)
      bc_doit(y)
    CompilerDefault
      CompilerError "[doit] Supported type"
  CompilerEndSelect
EndMacro

Define var.ab, ok.bc

doit(var)
doit(ok)
The good thing with this method is, that it is done on compiler-level without any additional code or memory used. Very Usefull, when you work with matrix and vectors...

As long as you don't use nested structures everything works perfectly.
User avatar
Psychophanta
Addict
Addict
Posts: 4968
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: SizeOf and nested structures

Post by Psychophanta »

Yes please, allow nested structures for SizeOf(), the C of the arduino does allow it. :idea: :)
I know, it might be hard but very useful at the end.
Another fast example:

Code: Select all

structure pepe
  jose.f
  juan.d
endstructure
Structure Person
  Name.s
  ForName.s 
  Age.pepe
EndStructure
var.person
Debug sizeof(person\Age); <- if this is allowed ...
;Debug sizeof(person\Age\juan); <- why this one is not?. At least it is not a "Syntax error" !
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Post Reply