Page 1 of 1

Compiler doesn't complain if pointer variable typed in struc

Posted: Wed Jun 05, 2019 7:46 pm
by Psychophanta
When pointer variable is defined inside a structure, compiler does not complain if the pointer is defined with no existing var-type.

Code: Select all

EnableExplicit

Structure var
  *m.DRI
EndStructure

define r.i,v.var
r.i=v.var\m
debug r

Re: Compiler doesn't complain if pointer variable typed in s

Posted: Wed Jun 05, 2019 8:16 pm
by Josh
No bug. A pointer is a pointer and for the structure it does not matter whether the type for the pointer is present or not.

If the structure would urge the pointer type, many things would not be possible, because no circle references for pointer variables would be possible any more.

Re: Compiler doesn't complain if pointer variable typed in s

Posted: Wed Jun 05, 2019 8:23 pm
by Psychophanta
I know, but what about a warning "not .DRI structure found in the complete code" ?
This topic should be placed in the 'feature requests' section :)

Re: Compiler doesn't complain if pointer variable typed in s

Posted: Wed Jun 05, 2019 8:49 pm
by Josh
PB as a single-pass compiler does not know at time of structure declaration what follows. On the other hand, I don't know where is your problem. At latest if you want to use the fields of your structure type 'DRI', you get an error message that DRI isn't defined.

Better is to move in the 'Coding Questions' section. :D

Re: Compiler doesn't complain if pointer variable typed in s

Posted: Thu Jun 06, 2019 9:33 am
by Psychophanta
Single pass compiling can detect the declaration or not declaration of any structure defined in the code.
I think nobody here must put their own problems with any issue, but the problem with any issue. I this case, I have not problem with this topic, but i see a strangeness with it.
We know we get an error when any field of undeclared structure (DRI) is used.

Re: Compiler doesn't complain if pointer variable typed in s

Posted: Thu Jun 06, 2019 10:03 am
by Fred
It's the intended behaviour to allow circular dependencies

Re: Compiler doesn't complain if pointer variable typed in s

Posted: Thu Jun 06, 2019 11:51 am
by HanPBF
Circular behaviour should have been done with declareStructure

No type check can stay because PureBasic is simply not a typed language:
procedure test(*S.MyStructure)
;...
endProcedure
define *T.AnotherStructure
test(*T) ; -> no compiler complain here
define T1.AnotherStructure
test(*T) ; -> no compiler complain here
As PureBasic is intended to be an easier C, the whole behaviour is correct.

Is it a really big problem?

Re: Compiler doesn't complain if pointer variable typed in s

Posted: Thu Jun 13, 2019 2:49 pm
by User_Russian
Fred wrote:It's the intended behaviour to allow circular dependencies
Then why in this code compiler reports an error?

Code: Select all

Declare Proc(*x.MyStruct)

Structure MyStruct
  x.l
  y.i
EndStructure

s.MyStruct
Proc(@s)

Procedure Proc(*x.MyStruct)
  If *x
    *x\x = 2
    *x\y = 4
  EndIf
EndProcedure

Re: Compiler doesn't complain if pointer variable typed in s

Posted: Thu Jun 13, 2019 3:15 pm
by Olliv
@UserRussian

Why

Code: Select all

Proc(@s)
?
s is already declared as a hidden pointor. Add @ prefix tells us the procedure won't treat x or y, but an other structure managing internal pointor. This last one not defined here.

Code: Select all

Proc(s)
seems to be the normal way.

Re: Compiler doesn't complain if pointer variable typed in s

Posted: Thu Jun 13, 2019 5:08 pm
by skywalk
I am not a fan of wasted lines and Declares.
I have ~100k lines of code with maybe 3 Declares.(Prototypes are not Declares.)
This issue is solved by proper ordering for a single pass compiler. :wink:

Code: Select all

;Declare Proc(*x.MyStruct)
Structure MyStruct
  x.l
  y.i
EndStructure
Procedure Proc(*x.MyStruct)
  If *x
    *x\x = 2
    *x\y = 4
  EndIf
EndProcedure
s.MyStruct
Proc(@s)

Re: Compiler doesn't complain if pointer variable typed in s

Posted: Thu Jun 13, 2019 5:53 pm
by Olliv
@Skywalk

Hello. Same remark as this one done previously.

Code: Select all

Structure aStruc
   a.I
   b.I
EndStructure

Define.aStruc X

Debug X
Debug @X
Results are equal...

But it is sure structures must be written before declarings using these last one.

We are now talking about details where everybody is right.

To go back to the subject, inserting a pointor in a structure :

Code: Select all

Structure var
   *a
EndStructure
This shows we stay free to choose whatever the pointor is pointing.

The pointor stays undefined and allows StructureUnion equivalents.

Re: Compiler doesn't complain if pointer variable typed in s

Posted: Thu Jun 13, 2019 11:27 pm
by Josh
Olliv wrote:@UserRussian

Why

Code: Select all

Proc(@s)
?
s is already declared as a hidden pointor. Add @ prefix tells us the procedure won't treat x or y, but an other structure managing internal pointor. This last one not defined here.

Code: Select all

Proc(s)
seems to be the normal way.
And what does this have to do with the contribution of User_Russian?

skywalk wrote:I am not a fan of wasted lines and Declares.
I have ~100k lines of code with maybe 3 Declares.(Prototypes are not Declares.)
This issue is solved by proper ordering for a single pass compiler. :wink:
Although this is not directly related to User_Russian's contribution, it should be up to each user to decide how to arrange his procedures. Personally I prefer to arrange my procedures as they belong together and that is not possible without 'Declare'.



I must fully agree with User_Russian:

Pb should just skip this idiotic error message for pointers in declare parameters and everything is fine. Fread doesn't need to do anything other than simply ignore the type of pointers in this case.


@User_Russian
It is better to eliminate all not necessary code in the examples. Then there will be no unnecessary discussions.

Re: Compiler doesn't complain if pointer variable typed in s

Posted: Fri Jun 14, 2019 1:32 am
by Demivec
@User_Russian:
User_Russian wrote:Then why in this code compiler reports an error?
The error can easily be removed with only a slight modification:

Code: Select all

;Declare Proc(*x.MyStruct)
Declare Proc(*x) ;The structure of a pointer is not required for a parameter of a Declare procedure.

Structure MyStruct
  x.l
  y.i
EndStructure

s.MyStruct
Proc(@s)

Procedure Proc(*x.MyStruct)
  If *x
    *x\x = 2
    *x\y = 4
  EndIf
EndProcedure

Re: Compiler doesn't complain if pointer variable typed in s

Posted: Fri Jun 14, 2019 4:32 am
by Little John
User_Russian wrote:
Fred wrote:It's the intended behaviour to allow circular dependencies
Then why in this code compiler reports an error?
Fred was not talking about Declare().

To avoid problems like in your example code above, just write all structure definitions before any Declare() statements.

Re: Compiler doesn't complain if pointer variable typed in s

Posted: Fri Jun 14, 2019 4:46 am
by Olliv
Josh wrote:And what does this have to do with the contribution of User_Russian?
You forget skywalk contribution : I do not know this technic about @StructuredVariable, not to say @StaticPointor.Integer .


For Declare:

Code: Select all

Structure void
EndStructure

Declare proc1(*a.void)
Declare proc2(*a)

Procedure proc1(*a.integer)
EndProcedure

Structure Something
   FirstOne.I
EndStructure

Procedure proc2(*a.Something)
EndProcedure
Above, I cannot see error occuring.
Plus, I can read << *a.integer >> in the tool bar when cursor is on << *a.void >>.
Even just *a does not cause an error.

Also...

Code: Select all

Structure aStruc
   *a.DontCare ; size of integer
   b.OtherStruc ; size of OtherStruc
EndStructure
Above we have a structure. We are fed up to manage Dontcare field address : we remove '*' character.
At the opposite : we want to manage OtherStruc which is a big structure during aStruc is often used to allocate lots of descriptors and so waste less memory : we insert '*' character. We prefer not to remove structure name to allow us to remember the thematic root of this field i.e. .

In a 'declare' line, this swap is unabled. But it is right that copy the bold following extract...

Procedure.D Op(A.D, B.D, *C.Status)

... to paste it below...

Decla <----- here

....is easy and quick. However, remove 'Status' can be useful to place declaration without worrying about structures place.

An other interest to keep a structure name near a structure field : knowing the initial configuration (circular or not).

And when we are a nut like me who uses first character of everything I find, MS for MainScreen, SoAs for Size Of AreaS, PsF for PreferenceS File, etc... Choose :

this:

Code: Select all

Structure IamFine
   *MS.MainScreen
   *SoAs.SizeOfAreas
   *PsF.PreferencesFile
EndStructure
or that:

Code: Select all

Structure IamTired
   *MS
   *SoAs
   *PsF
EndStructure