Better way to do BYTEPointer[0]=100?

BlitzMax Forums/BlitzMax Programming/Better way to do BYTEPointer[0]=100?

Damien Sturdy(Posted 2007) [#1]
Hi All,

so, um, is there no way to get a variable which is equivalent to:
BYTEPointer[0]


so that, for example, I can

NewVariable=BytePointer[0]

NewVariable=100


which means when I set NewVariable, BytePointer[0] gets set too? Basically, I need NewVariable to be the variable pointed to by BytePointer.


rdodson41(Posted 2007) [#2]
If a pointer points to a variable, then seting BYTEPointer[0] will change the variable as changing the variable will change BYTEPointer[0].

SuperStrict

Local b:Byte = 100
Local bp:Byte Ptr = Varptr(b)

Print b
Print bp[0]

bp[0] = 150

Print b
Print bp[0]



Damien Sturdy(Posted 2007) [#3]
I'm using pointers from C++. C++ returns a pointer, I need to set a variable so that if this pointer is changed, so is the variable :) can it be done?


Dreamora(Posted 2007) [#4]
You are working in a managed language so no unless you are willing to add instability and undebugeable bugs.

There are references and paradigms to make this kind of "pointer hack" working in a managed environment as well. They might look more complicated upfront, but at least they don't shoot holes in the OS and crash it :)


gman(Posted 2007) [#5]
greetings :) i believe what you want to store is a pointer to the CPP pointer. im pretty sure as long as your CPP pointer doesnt fall out of scope (making your pointer to it invalid) this should work just fine.

handle_ptr.bmx
Framework BRL.Basic
Import "handle_ptr.cpp"

Print("in CPP, theValue is initially 22 and theValue2 is initially 33")
Print("")
Print("retrieving handle pointer...")
Local my_ptr:Int Ptr Ptr=getHandlePtr()
Print("value of theValue in CPP is: "+my_ptr[0][0])
Print("")
Print("changing the value to 99...")
setNewValue(99)
Print("value of theValue in CPP is now: "+my_ptr[0][0])
Print("")
Print("changing the CPP handle pointer to now point to theValue2...")
changePtr()
Print("value of theValue2 in CPP is: "+my_ptr[0][0])
Print("")
Print("setting the value from BMX to 321")
my_ptr[0][0]=321
Print("value of theValue2 in CPP is now: ") 
showOut()
Print("")

Extern 
	Function getHandlePtr:Int Ptr Ptr() ' returns a pointer to the CPP handle (pointer)
	Function setNewValue(newVal:Int) ' sets theValue to a new value
	Function changePtr() ' changes the internal CPP handle to point to theValue2 instead of theValue
	Function showOut() ' displays the value of theValue2
EndExtern


handle_ptr.cpp

#include "stdio.h"

int theValue=22;
int theValue2=33;

// store a pointer to the first int variable
int* handle=&theValue;

extern "C" {

	// returns the handle to the pointer (a pointer to the pointer)
	int** getHandlePtr() 
	{
		return &handle;
	}
	
	// sets a new value for the first int variable
	void setNewValue(int newVal)
	{
		theValue=newVal;
	}
	
	// changes to point to the second int variable
	void changePtr()
	{
		handle=&theValue2;
	}

	// prints out the value of theValue2
	void showOut()
	{
		printf("%d",theValue2);
	}

}


hope this helps you out :) good luck!


Damien Sturdy(Posted 2007) [#6]
Hey cool, thanks- However, I think I was after the Opposite.

Imagine in C++, a float. I can send the pointer to this to max. Instead of typing ReturnedVar[0]=1024, i'd like to just type var=1024 and have everything deal with itself.

However, I thought a bit deeper about this and it seems Dreamora would be correct- I see many ways it can mess up the GC.

Thanks for your help guys.


gman(Posted 2007) [#7]
ah yes sorry :) for that you would need to do the opposite. you would need to start with a variable in BMAX and give its pointer to C++. your still dealing with a pointer, just shifting it to the CPP side. im not sure how safe that would be though. the big question would be does BMAX ever internally rearrage variable memory locations?


ImaginaryHuman(Posted 2007) [#8]
You could perhaps mess with the variable pointer of a variable and make it point to an element in the byte array, or you could make a static bank that points to the array as its memory space so you can then use peek/poke to set the value, or at least use that to change the varptr of the variable to point to the array content.


grable(Posted 2007) [#9]
I think what he wants is not doing [0] for dereferencing a pointer.. sorry mate but nearly all languages that has pointers has some kind of syntax for that.

If not, they have a special syntax for setting the pointer instead ;)


rdodson41(Posted 2007) [#10]
I would say just keep track of the pointer and assign and retrieve values from it useing [0]. That's the only solution I can think of with the pointer coming from C++ to BMX and not the other way around.