Page 1 of 1

Direct memory Pointer with structure

Posted: Mon Sep 23, 2024 2:21 am
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

Re: Direct memory Pointer with structure

Posted: Mon Sep 23, 2024 2:54 am
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


Re: Direct memory Pointer with structure

Posted: Mon Sep 23, 2024 3:50 pm
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	

Re: Direct memory Pointer with structure

Posted: Mon Sep 23, 2024 4:24 pm
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	

Re: Direct memory Pointer with structure

Posted: Mon Sep 23, 2024 8:32 pm
by idle
In that case use copystructure might be a bit more useful if you have objects in structures.

Re: Direct memory Pointer with structure

Posted: Mon Sep 23, 2024 10:55 pm
by GenRabbit
Thanks, that worked too.