Page 1 of 1

Storing Pointers

Posted: Tue Apr 13, 2004 4:33 am
by Derlidio
Yeepy...

I was sketching some points related to the execution flow of an app I'm working on, and get to a concept question: Is it safe to store a pointer to a procedure for future use? (never worked with pointers before :oops: )

Lets say that I create an array of pointers, each one pointing to a procedure within my code. How long can I trust those pointers? My concern is about OS memory management. Will Windows (or Linux) allways keep my App EXE within the same memory area? Can I trust that the pointers will be pointing to my procedures despite of how many apps are loaded/unloade from system memory while my app is running?

I know it is a basic concept (and I apologize in advance for this post if it is in the wrong place) but I must to know about it in order to proceed coding. Any clue will be highly apreciated.

Thanks...

Posted: Tue Apr 13, 2004 9:32 am
by GedB
Derlidio,

The lifetime of your pointers is dependant upon the lifetime of the item it points to.

A pointer is just the address in memory. For example, a procedure is complied into machine code. The complied form is stored as a series of numbers that can be executed by the CPU. A pointer to a procedure contains the memory address for the first byte of that procedure. A pointer is just a long variable with that address. The * in front of the name just lets the compiler know that it is a pointer, and not to be type checked as a long.

Think of it as being like a database where records are stored with a unique key. For example, you might have a customer number of 12345. When you phone up the company you quote 12345 and they can retrieve your details.

12345 contains no information in itself. If there was an accident and the company deleted your records then quoting 12345 will not help them at all. Even worse, it may have been allocated to a different customer causing even more confusion.

Anyway, the important point is that the pointer is just the key used to access something held in memory. It is the item in memory that is important, not the pointer to it.

Procedures, Arrays and Linked Lists are all Global, so they will always be available as long as the application is running. Purebasic obtains the memory that these are stored in for you, and does not release them until the application exits.

The same is true for variables declared in the main body of the application, or that are declared Global. The will always remain 'live' until the application finishes.

Memory that is allocated by yourself is available to the application's finish, or until you free or reallocate it.

The one place you have to be careful is when a variable is declared within a procedure. These are created on the stack, and released as soon as the procedure finishes.

This only applies to the variables. Global items like a Linked List can be safely created inside a procedure. The same goes when allocating memory.

I'm not sure what the status of the new Static variables are. I suspect that a pointer to one of these will remain valid outside the procedure, even though it's name has gone out fo scope.

Posted: Tue Apr 13, 2004 5:55 pm
by Soulfire
GedB, you must not have understood his question. He wants to know if pointers to data elements will be valid after a given amount of time. For instance, let's say your program creates a pointer to a variable at address 0xF981B34. After your program has been running a while, something happens that requires the OS to shuffle around some memory to make room for new data(a new program was opened that needs lots of ram, or something of the like). So the OS moves the variable your program created to a new location in memory to make room for this new program. However, your pointer to that variable remains unchanged, so it thinks the variable is still at 0xF981B34, when in fact the OS moved the variable's data to 0x819C9A! Therefore, the pointer is now invalid, and if you try dereferencing it your program will throw a memory access violation error.

Of course, I have never seen this happen... I'm not even sure this happens at all. Just wanted to clear up what Derlidio was asking.

Posted: Tue Apr 13, 2004 6:07 pm
by El_Choni
AFAIK, you can use those pointers safely. The Windows PE loader loads your executable in memory and 'maps' it to an address, fixing all internal pointer references. This means your executable is assigned an address in memory and it doesn't change, no matter if your file is moved from phisycal memory to hd memory or elsewhere.

If memory runs low? I don't know, I guess memory allocations would fail and Windows won't be able to run any more exes until memory is released.

Posted: Tue Apr 13, 2004 6:27 pm
by dell_jockey
it's my understanding that the OS virtual memory manager (VMM) should take care of that problem in case code that is referenced by a pointer gets moved in physical memory.

Posted: Tue Apr 13, 2004 6:51 pm
by freak
Yes, the OS takes care of all this. Your program has a virtual memory space. None of these adresses are physical ones, so when the OS moves
memory around, nothing changes for your program.

It is the same with memory you get from AllocateMemory(). It's adress
will stay the same forever, unless you change it with FreeMemory() r ReAllocateemory()

Timo

Posted: Tue Apr 13, 2004 6:58 pm
by Derlidio
Thanks everybody for your answers!

GedB gave a very good pointers explanation. I think it will be usefull not olny for me but for anybody who haves questions about pointers :wink:

Indeed, my doubts was not related to pointers concept itself, but to their lifetime. Soulfire has got the point here. What I want to do is to store pointers to procedures within a structured linked list. Each element of this list will hold information about an object in the screen + a pointer to a function. Each object may be handled by a different function in code. When the program needs to update the state of these objects I intend to call the procedures related to each one by its address (using CallFunctionFast), so I dont have to deal with Select/If statements to know what function must be called.

I hope Windows (and Linux) works as El_Choni and dell_jockey told in their posts. Well, lets code :D

Thanks you all...

Posted: Tue Apr 13, 2004 7:02 pm
by Derlidio
And here comes a big Thanks to Freak! The confidence you show on your post makes me feel a lot more confortble :D

Thanks a lot..