Compiler doesn't complain if pointer variable typed in struc

Just starting out? Need help? Post your questions and find answers here.
User avatar
Psychophanta
Addict
Addict
Posts: 4975
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Compiler doesn't complain if pointer variable typed in struc

Post 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
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

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

Post 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.
sorry for my bad english
User avatar
Psychophanta
Addict
Addict
Posts: 4975
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

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

Post 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 :)
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

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

Post 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
sorry for my bad english
User avatar
Psychophanta
Addict
Addict
Posts: 4975
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

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

Post 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.
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post by Fred »

It's the intended behaviour to allow circular dependencies
HanPBF
Enthusiast
Enthusiast
Posts: 563
Joined: Fri Feb 19, 2010 3:42 am

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

Post 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?
User_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

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

Post 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
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

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

Post 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.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post 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)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

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

Post 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.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

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

Post 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.
sorry for my bad english
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

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

Post 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
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

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

Post 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.
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

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

Post 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
Post Reply