Can someone explain this please? (RESOLVED)

Just starting out? Need help? Post your questions and find answers here.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Can someone explain this please? (RESOLVED)

Post by srod »

The following runs fine:

Code: Select all

Dim a(10)
Dim b(10)

a(0) = 1
a(1) = 2

b()=a() ;b should now point to a.

Debug b(1) ;Displays 2 as expected.
However, the following does not display the correct result. It should be functionally identical to the above.

Code: Select all

Dim a(10)

a(0) = 1
a(1) = 2

Procedure Test(addy)
  Dim b(10)
  b()=addy
  Debug b(1)  ;Should display 2 !
EndProcedure

Test(a())
??? I'm probably doing something daft! :)
Last edited by srod on Tue May 16, 2006 10:49 pm, edited 1 time in total.
I may look like a mule, but I'm not a complete ass.
Tranquil
Addict
Addict
Posts: 950
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

Are you sure that A() = B() is a valid operation!? Never thought about this. Sure, A() returns something like a pointer. Is this new on PB4?
Tranquil
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

Remember that Dim is no longer Global by default in PureBasic 4.

Did you try adding Global in front of both your Dim commands so the data can be shared?
Image Image
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Thanks for the replies.

Paul: I wish to avoid using global arrays at the moment, but that would of course solve this problem - although it doesn't explain why the code doesn't run as is!

Tranquil: no, it's not new on PB4, I used to use this all the time with PB3.94 etc. Although this was of course where all arrays were global etc.

Strange, the statement

Code: Select all

b()=a()
has no affect when used in a procedure, even if both arrays are DIMensioned within the procedure itself. This would appear to be the root of the problem.
I may look like a mule, but I'm not a complete ass.
Tranquil
Addict
Addict
Posts: 950
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

I searched through the whole PB referenze and I can not find any hint that your expression is valid.

Maybe its a bug that it even works. Normaly the () indicates a linked list.
Tranquil
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

Well, be quiet before Fred fixes that bug :lol:
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I think it's well known that a() gives the base address of the array. Indeed, in PB4, you use this to pass an array to a procedure. The expression b()=a() is probably not documented (or encouraged :lol: ), however I've seen it used quite a lot.

Oh well, I'll need to take another tact all together.
I may look like a mule, but I'm not a complete ass.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

--Kale

Image
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

If you were going to code that, I believe this would be right:

Code: Select all

Dim a(10) 

a(0) = 1 
a(1) = 2 

Procedure Test(addy(1)) 
  Dim b(10) 
  b()=addy()
  Debug b(1)  ;Should display 2 ! 
EndProcedure 

Test(a()) 
However, it fails because even though the procedure knows the correct address of the a() array, it refuses to assign it to b() because they do not share the same scope. So I don't believe it is expected to work.
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Yea, I looked at that. It could be (at least in that I guess the code I'm using is probably to be discouraged!) Still, it's strange that the 'trick' works outside of procedures, but not within. :)
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

netmaestro, the following doesn't work either although both arrays share the same scope:

Code: Select all

Procedure Test()
Dim a(10)
Dim b(10)

a(0) = 1
a(1) = 2
b()=a()

Debug b(1)  ;Should display 2 !
EndProcedure

test()
I may look like a mule, but I'm not a complete ass.
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

Try with Macro? maybe it works
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

You probably won't like this because it copies the whole array, but I'll offer it anyway just in case:

Code: Select all

Dim a(10) 

a(0) = 1 
a(1) = 2 

size = PeekL(@a()-8) * SizeOf(long)

Procedure Test(loc,size) 
  Dim b(10) 
  CopyMemory(loc,b(),size)
  Debug b(1)  ;Should display 2 ! 
EndProcedure 

Test(a(),size) 
BERESHEIT
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

This works:

Code: Select all

Dim a(10) 

a(0) = 1 
a(1) = 2 

Procedure Test(addy) 
  Static Dim b(10) 
  b()=addy
  Debug b(1)  ;Should display 2 ! 
EndProcedure 

Test(a()) 
BERESHEIT
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

so it's all about using static, right?

these *undocumented* things drive me crazy
Post Reply