Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Manipulating a variable through memory?

Author
Message
Vandetta
21
Years of Service
User Offline
Joined: 17th Dec 2002
Location:
Posted: 22nd Dec 2002 06:02
Ok, look at this little code segment:

a=5
b=3
print a, b
ReadConfig()

I dont know if there's a way to do this, but I want to have ReadConfig change the values of a and b without a and b being global variables. Is there any way to do this. Basically I was thinking of something like figuring out where the variables are stored in memory and write to that area of memory, so I would manipulate an area of memory but not the variable itself.
MrTAToad
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 22nd Dec 2002 12:22
Unfortunately, no there isn't a way. A way around it is to allocate some memory; put the variables into that; pass the memory location to your function and let it modify the memory accordingly. Once the function has finished, read the memory and put back into the variables.

Good news everyone! I really am THAT good...
Oh my, yes...
ICERGB
21
Years of Service
User Offline
Joined: 8th Nov 2002
Location: Canada
Posted: 22nd Dec 2002 14:39
Oh ya! and this came with the "stinking" manual!
ICERGB
21
Years of Service
User Offline
Joined: 8th Nov 2002
Location: Canada
Posted: 22nd Dec 2002 14:43
Oh, I got an idea; how about we put something in memory!

Ask DBDN !!!
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 22nd Dec 2002 14:57
There is no way to do this without *something* being global. I would use a memory area or memblock like MrTAToad suggested, and then use functions to get and set the values. If you wanted the variable reads to look like standard variable accesses, you could define a constant to do the substitution to a function for you:

#constant MyVariable MyVariableFunction()
Barliesque
21
Years of Service
User Offline
Joined: 10th Nov 2002
Location: Los Angeles
Posted: 27th Dec 2002 23:44
As an alternative to memblocks, you might want to try using "indirection." I thought for a moment this would let you do just what you're trying to do, but alas, there's still no way of getting the address in memory of a standard variable.

The following comes from DarkBASIC's help (see: "Principles >> Datatypes and variables")

You can use the value of a variable to specify, read and write the contents of an address by using the '*' indirect symbol. This is useful for obtaining areas of memory you wish to read from or write to and control the pointer into this memory using a standard variable.

Ptr as DWORD
Ptr=0x00FF8820
*Ptr=42
PRINT *Ptr

The above code simply declares Ptr as a DWORD variable, assigns the variable a random area of memory, writes the value 42 into that location of memory and then reads the same address back, the contents of which is to be printed to the screen. You would never use an absolute address to assign a pointer. There are commands such as GET BACKBUFFER PTR and MAKE MEMORY which return such address values. You can only use a standard variable with indirection, and array and type variables are not valid. This is an advanced feature and as such potentially dangerous. Writing into invalid memory can cause unexpected program behaviour and even crashing. Use caution when employing the indirect symbol.
MrTAToad
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 27th Dec 2002 23:51
I do hope the put in an addressof (&) command... it would be so useful...

Good news everyone! I really am THAT good...
http://www.nickk.nildram.co.uk/ for great plug-ins - oh my, yes!

Login to post a reply

Server time is: 2024-05-17 07:20:01
Your offset time is: 2024-05-17 07:20:01