
Basic Data Operation

This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:


Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses




Task

Demonstrate how to get the address of a variable and how to set the address of a variable.




 
== Get ==
 
To get the address of a variable without using the Windows API:
 
DEF X:INT
DEF pPointer:POINTER
pPointer=X
 
----
 
To get the address of a variable using the Windows API Lstrcpy function called in Creative Basic:
(This may give users of another language without a native way to get the address of a variable to work around that problem.)
 
DEF Win:WINDOW
DEF Close:CHAR
DEF ScreenSizeX,ScreenSizeY,Col:INT
 
'***Map Function***
DECLARE "Kernel32",Lstrcpy(P1:POINTER,P2:POINTER),INT
'The pointers replace the VB3 variable type of Any.
 
'Note: This is translated from VB3 or earlier code, and "Ptr" is *not* a Creative Basic pointer. 
DEF Ptr:INT
DEF X1:INT
DEF X2:STRING
 
X1=123
 
'***Call function***   
Ptr=Lstrcpy(X1,X1)
 
GETSCREENSIZE(ScreenSizeX,ScreenSizeY)
 
WINDOW Win,0,0,ScreenSizeX,ScreenSizeY,@MINBOX|@MAXBOX|@SIZE|@MAXIMIZED,0,"Skel Win",MainHandler
 
'***Display address***
PRINT Win, "The address of x1 is: " + Hex$(Ptr)
      X2="X2"
 
WAITUNTIL Close=1
 
CLOSEWINDOW Win
 
END
 
SUB MainHandler
 
	SELECT @CLASS
 
	CASE @IDCLOSEWINDOW
 
	Close=1
 
	ENDSELECT
 
RETURN
 
Note: The Windows Dev Center (http://msdn.microsoft.com/en-us/library/windows/desktop/ms647490%28v=vs.85%29.aspx) says
improper use of the Lstrcpy function may compromise security. A person is advised to see the Windows Dev site before using
the Lstrcopy function.
 
== Set ==
 
It appears to the author the closest one can come to setting the address of a variable is to set which bytes will be
used to store a variable in a reserved block of memory:
 
DEF pMem as POINTER
pMem = NEW(CHAR,1000) : 'Get 1000 bytes to play with
#<STRING>pMem = "Copy a string into memory"
pMem += 100
#<UINT>pMem = 34234: 'Use bytes 100-103 to store a UINT
DELETE pMem
 