Global vs. Shared variables

Just starting out? Need help? Post your questions and find answers here.
Tipperton
Addict
Addict
Posts: 1286
Joined: Thu Jun 19, 2003 7:55 pm

Global vs. Shared variables

Post by Tipperton »

What exactly is the difference between a global variable and a shared variable?

The way I read it from the help file they appear to be the same thing.
Last edited by Tipperton on Mon Jul 07, 2003 1:28 pm, edited 1 time in total.
plouf
Enthusiast
Enthusiast
Posts: 281
Joined: Fri Apr 25, 2003 6:35 pm
Location: Athens,Greece

Post by plouf »

when you create a Global variable it is accesible everywhere in your program

a shared declared inside a procedure and it is a variable of your main program accessible inside this procedure only

i have problem to understand it clearly in beggining too ;)
until someone explain it to me
Christos
ebs
Enthusiast
Enthusiast
Posts: 557
Joined: Fri Apr 25, 2003 11:08 pm

Post by ebs »

I agree with you; in PB there isn't much difference at all. Shared variables are like static variables in C - they retain their value between procedure calls. However, they are accessible from the main program, which is not the case in C.

A simple example:

Code: Select all

Procedure.l AddOneShared()
  Shared SValue.l
  
  SValue + 1
  ProcedureReturn SValue
EndProcedure

Debug "Shared = " + Str(SValue)
Debug ""

For n = 1 To 10
  Debug "Shared = " + Str(AddOneShared())
Next

Debug ""
Debug "Shared = " + Str(SValue)

SValue = 99

Debug ""
Debug "Shared = " + Str(SValue)

Debug ""
Debug "Shared = " + Str(AddOneShared())

Debug ""
Debug "Shared = " + Str(SValue)
Notice that the value of SValue can be read and set from the main program.

Regards,
Eric
Andy
User
User
Posts: 18
Joined: Wed Jun 18, 2003 8:32 pm
Location: Switzerland

Shared have to be in main?

Post by Andy »

Hello again

I have here still a question to shared variables: It seems to be, that shared variables >have< to be known in the main program:

Code: Select all

a.l = 10
;b.l=30
;run the program one time without the definition "b.l=30"
;and another time with it and you can see...

Procedure p2()
  Shared a, b
  Debug "p2 ------"
  Debug a 
  Debug b 
EndProcedure 

Procedure p1()
  Shared a 
  ;Another possibility to make this code running "correct":
  ;Shared a,b
  b.l=20
  Debug "p1 ------"
  Debug a 
  Debug b 
  p2()
EndProcedure 

p1()

End
Am I right - the shared have to be in the main, it's not possible to make a shared, which is only known in the procedures?

Best Regards
Andy
ebs
Enthusiast
Enthusiast
Posts: 557
Joined: Fri Apr 25, 2003 11:08 pm

Post by ebs »

Andy,
It seems to be, that shared variables >have< to be known in the main program:
I don't think that's exactly correct. In your example, variable a is shared in both procedures p1 and p2. Therefore, when you set it's value in the main program, it can be "seen" in both procedures. However, variable b is shared only in procedure p2. When you set the variable b in procedure p1, you are setting the value of a local variable, visible only in that procedure.

If you want to demonstrate that a variable can be shared in two procedures but not in the main program, try this version of your example:

Code: Select all

;a.l = 10  <-- commented out
;b.l=30
;run the program one time without the definition "b.l=30"
;and another time with it and you can see...

Procedure p2()
  Shared a, b
  Debug "p2 ------"
  Debug a
  Debug b
EndProcedure

Procedure p1()
  Shared a
  ;Another possibility to make this code running "correct":
  ;Shared a,b
  a.l=5  ; <-- new line 
  b.l=20
  Debug "p1 ------"
  Debug a
  Debug b
  p2()
EndProcedure

p1()

End
Since the first line is commented out, variable a is shared in both procedures p1 and p2, but not in the main program. Run the program and you will see that when the value of 5 is assigned to a in p1, a has the same value in p2. This is the same thing as using the statement "Shared a,b" (to share variable b) in procedure p1, as you show in your example.

I'm not sure if this explanation makes clear what I'm trying to say, so let me know if this makes sense.

Regards,
Eric
Andy
User
User
Posts: 18
Joined: Wed Jun 18, 2003 8:32 pm
Location: Switzerland

Shared have to be in main?

Post by Andy »

Thank you Eric, for your anwser.

But - sorry - I do not understand :? ...

Also in your code will "a" be know in the main - or with other words, I can't use an "a" in the main, without that the "a" in p1() will not also change it... (I hope you can understand my terrible english :oops:). The "b" is only known in p1() - not in p2() - despite the "shared a,b" command...
I think now, a better description will be, that "shared" is a kind of "global", but only for the procedure where "shared" is used. What do you mean?

Regards
Andy
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

> I think now, a better description will be, that "shared" is a kind
> of "global", but only for the procedure where "shared" is used. What do
> you mean?

Yes, shared makes a variable accessible in the main program, and in
all procedures, where this variable is declared as shared.

Timo
quidquid Latine dictum sit altum videtur
Post Reply