Direct memory Pointer with structure

Just starting out? Need help? Post your questions and find answers here.
GenRabbit
Enthusiast
Enthusiast
Posts: 151
Joined: Wed Dec 31, 2014 5:41 pm

Direct memory Pointer with structure

Post by GenRabbit »

Can this be done at all?
I know you can use CopyMemory/peek/poke.
This line stops it; 'Protected.Newtest Attempt = ?Mystructure'
This is for reading only, not writing to.

Code: Select all

EnableExplicit
Structure NewTest
	Value1.l
	Value2.l
	Value3.l
	Value4.l
EndStructure

DataSection
	MyStructure:
	Data.l 4,16,64,256
EndDataSection

Define nl.NEWTEST

Procedure loadConfig(*t1.newtest)
	Protected.Newtest Attempt = ?Mystructure
	Protected.i x
	x = ReadFile("JustChecking.txt"
	If x 
		; do lots of stuff
		CloseFile(x)
	Else
		*tl = *Attempt
	EndIf
	
	;*t1\Value1=1	
	;*t1\Value2=2	
	;*t1\Value3=4
	;*t1\Value4=8
EndProcedure

loadConfig(@nl)
Debug nl\Value1
Debug nl\Value4
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Direct memory Pointer with structure

Post by idle »

you can do it like this. just overwrite the structure in the datasection

Code: Select all

EnableExplicit
Structure NewTest
	Value1.l
	Value2.l
	Value3.l
	Value4.l
EndStructure

DataSection
	MyStructure:
	Data.l 4,16,64,256
EndDataSection

Global *nl.NEWTEST = ?Mystructure 

Procedure loadConfig(*t1.newtest,bSimReadfile=0)
	
	Protected.i x
		
	x = bSimReadfile ;ReadFile(-1,"JustChecking.txt")
	If x 
	  
	  *t1\Value1=1	
	  *t1\Value2=2	
	  *t1\Value3=4
          *t1\Value4=8
	  	  
	  ; do lots of stuff
		;CloseFile(x)
	  
	EndIf
	
	
	
EndProcedure

loadConfig(*nl,0)
Debug *nl\Value1
Debug *nl\Value4

loadConfig(*nl,1)
Debug *nl\Value1
Debug *nl\Value4

GenRabbit
Enthusiast
Enthusiast
Posts: 151
Joined: Wed Dec 31, 2014 5:41 pm

Re: Direct memory Pointer with structure

Post by GenRabbit »

Thanks, I wasn't even sure it was possible. This is awesome.
Here is my new code. Now I need to find out why the procedure doesn't return its values.
---------------------- Update!
*lc = *Attempt. Obvious. This copied the pointer, not its content. Gah, should have guessed that long ago.

Code: Select all

EnableExplicit
Structure NewTest
	Value1.l
	Value2.l
	Value3.l
	Value4.l
EndStructure

DataSection
	MyStructure:
	Data.l 4,16,64,256
EndDataSection

Define myNewtest.NEWTEST

Procedure loadConfig(*lc.newtest)
	;Protected.Newtest *Attempt = ?Mystructure
	Protected *Attempt.NEWTEST = ?Mystructure
	Protected.i x
	x = ReadFile(#PB_Any,"JustChecking.txt")
	If x 
		; do lots of stuff
		CloseFile(x)
	Else
		*lc = *Attempt
	EndIf
	
	Debug *lc\Value1
	Debug *lc\Value2
	Debug *lc\Value3
	Debug *lc\Value4
	Debug "---------------------------------------------"
EndProcedure

loadConfig(@myNewtest)
Debug myNewtest\Value1
Debug myNewtest\Value2	
Debug myNewtest\Value3	
Debug myNewtest\Value4	
GenRabbit
Enthusiast
Enthusiast
Posts: 151
Joined: Wed Dec 31, 2014 5:41 pm

Re: Direct memory Pointer with structure

Post by GenRabbit »

Seems I was a little quick with hurrayh it works. Had to go back to copymemory anyway. So it didn't work as I hoped. But it was a learning experience. :)

Code: Select all

EnableExplicit
Structure NewTest
	Value1.l
	Value2.l
	Value3.l
	Value4.l
EndStructure

DataSection
	MyStructure:
	Data.l 4,16,64,256
EndDataSection

Define myNewtest.NEWTEST

Procedure loadConfig(*lc.newtest)
	;Protected.Newtest *Attempt = ?Mystructure
	Protected *Attempt.NEWTEST = ?Mystructure
	Protected.i x
	x = ReadFile(#PB_Any,"JustChecking.txt")
	If x 
		; do lots of stuff
		CloseFile(x)
	Else
		;*lc = *Attempt
		CopyMemory(?mystructure, *lc,SizeOf(newtest))
	EndIf
	
	Debug *lc\Value1
	Debug *lc\Value2
	Debug *lc\Value3
	Debug *lc\Value4
	Debug "---------------------------------------------"
	Debug *lc
	Debug *Attempt
	Debug "---------------------------------------------"
	
EndProcedure

loadConfig(@myNewtest)
Debug myNewtest\Value1
Debug myNewtest\Value2	
Debug myNewtest\Value3	
Debug myNewtest\Value4	
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Direct memory Pointer with structure

Post by idle »

In that case use copystructure might be a bit more useful if you have objects in structures.
GenRabbit
Enthusiast
Enthusiast
Posts: 151
Joined: Wed Dec 31, 2014 5:41 pm

Re: Direct memory Pointer with structure

Post by GenRabbit »

Thanks, that worked too.
Post Reply