handles and variables

Blitz3D Forums/Blitz3D Beginners Area/handles and variables

_PJ_(Posted 2003) [#1]
I am not sure what the effect is of the following code.

image_handle=LoadImage("image.bmp")
variable=image_handle


What exactly does 'variable' now represent? does it act like a hanlde of a handle and 'point' to the image, or does it just represent the number which the handle is defined as.

According to the debugger, and
Print variable
it should jus be a number, so how come

DrawImage variable,0,0 

works???


This is quite important to me, as I will need to be able to go through all my 3D entities and select some of them. To do this, I need to be able to assign a variable which will represent the selected entity.


Tricky(Posted 2003) [#2]
The variable is to be compared with a pointer... It doesn't contain the data itself, only the address of the memory bank it's stored in...

That's how it works...


To get this in further technobabble
G = LoadImage("Pic1")
G = LoadImage("Pic2")

In this code, the second command doesn't overwrite the first loaded pic... Only the var that points to the memory, making that the first pic is still in the memory, but no longer accessible since the address is lost... (We got the command FreeImage to cover that problem)...


But to keep things relevant, the picture is stored in a memory bank and the variable only contains the memory address of that bank...


jhocking(Posted 2003) [#3]
Handles are just integer values. Specifically the word "handle" is just a fancy way to say "a variable which stores the address to a specific location in memory." Thus assigning a variable to an image's handle is essentially creating a second handle for that image.

As for why using DrawImage with your variable works, that is because the command assumes the first number is a handle for an image. Thus, no matter what number you put there, it will look for an image at that address. If it finds one great, if not you get an error.

**********

ADDITION: Cross posted answers. His analogy with pointers is correct (actually, come to think of, a handle IS a pointer. Meanwhile another good analogy I can think of is a website URL. The URL text simply refers to a router number which is the location of the website.


_PJ_(Posted 2003) [#4]
is there a way of telling the computer where to put the image when first loaded in?

I mean, rather than let the computer define the memory bank location, is there a way of setting up so that Image xxx is loaded into bank zzzzzzzzzzz
obviously being careful to keep enough space between Image xxx and Image yyy

?


jhocking(Posted 2003) [#5]
I doubt it. That Blitz does memory management of that sort automatically is a large part of why it is useful. Why would you want to do that anyway?


SSS(Posted 2003) [#6]
All memory is managed automaticly, placing memory in my knowledge is impossible even in C++, you must use and asm tag...


SJB(Posted 2003) [#7]
In C++, C, Pascal, etc. you can allocate memory with functions like malloc, realloc, calloc, not to mention all the memory management functions in Windows. The programmer is then free to use the memory for whatever they wish. Thus things can be arranged in memory as desired. Implementing sub-allocators is not uncommon.

Things like placement new, and the ability to replace the allocator for new, allows the developer to place C++ objects in specified memory locations.


Tricky(Posted 2003) [#8]
Blitz has direct memory access, but it's advisable not to use it, since you mostly don't need it... Features enough to take that kind of stuff out of your hands....