Page 1 of 1

Global Pointers

Posted: Wed Apr 09, 2008 1:20 pm
by Capella
Can a Pointer be Global?

I need to use pointers in procedures (from where imported functions are called) and do not want to pass them as parameters, but instead have access to them as global.

It works fine as long as I have the call for the imported function in the main program, but when I move the same to a procedure problem occur.

I have the following code:

Code: Select all

;-------------------------------------------------
Import "calc32.lib"
   julday (a.l, b.l, c.l, d.d, e.l, f.l=0) As "_julday@24"
EndImport
Prototype.d FunctionA (a.l, b.l, c.l, d.d, e.l, f.l=1)
*PointerA.FunctionA=@julday()
;-------------------------------------------------
juliandayUT.d=*PointerA (year.l, month.l, day.l, time.d, flag.l)
;---------------------------------------------------
But if I now declares the pointer as global as follows...

Code: Select all

;---------------------------
Global *PointerA.FunctionA=@julday()
;--------------------------
...and move the last line to a procedure like this...

Code: Select all

;----------------------------------------
Procedure Procedure JulianDate()
   juliandayUT.d=*PointerA (year.l, month.l, day.l, time.d, gregflag.l)
EndProcedure
;---------------------------------------------------
...and call that procedure with a...

Code: Select all

;--------------------------------------
JulianDate()
;------------------------------------
It stops with a invalid memory access.

So I assumes that there's some problem with the pointer here. Can anyone explain the reason for this problem and how it should be solved?

Posted: Wed Apr 09, 2008 1:33 pm
by dhouston
I have a scenario where I send many different packets of data via RS232. The last byte in each packet is an 8-bit checksum of the preceding bytes. I use a global pointer to a buffer. Several procedures use it to allocate memory and build the packets and then call the Checksum procedure which uses it to sum the bytes and plug-in the checksum. It all seems to work as the embedded device at the other end of the RS232 link is happy with all the packets. Your scenario is a bit more complex and I suspect it's something related to that complexity that is the source of your problem rather than global pointers per se.

Posted: Wed Apr 09, 2008 1:39 pm
by tinman
Yes, a pointer can be global.

But you seem to have your import defined wrongly, since you are passing 28 bytes into it but the function name states on 24. So when you call this in a procedure you are getting the stack messed up and end up with an invalid memory access.