KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Pointers and Local Variables
PRODUCT: 4D | VERSION: | PLATFORM: Mac & Win
Published On: November 19, 1999

There is one important fact to remember about local variables and pointers. Never use a pointer to reference (point to) a local variable. The reason is based on the way variables are handled in memory. A local variable is located in a type of memory called a Stack. A stack is defined a place to store temporary values. 4D makes frequent changes to the stack and the stack pointer and can move the stack pointer below memory locations holding local variables. This will cause the memory to be re-used and their values overwritten with new data . Therefore, the stack should be considered temporarily usable memory. You can not have a pointer to anything located in temporary memory.

The result is that a pointer might or might not work correctly if it points to a local variable. It depends if 4D has made changes to the stack or not. The result of attempting to dereference a pointer (using the pointer to access the variable it points to) that points to the wrong address is that the program will crash or that the data being read or written is wrong.

Process and Interprocess variables are stored in regular memory (referred to as the Heap). When memory moves happen the addresses for Process and Interprocess variables are properly updated.

Examples of pointing to variables

Note: For more information about variables consult the Variable section of the Language chapter of the Language Reference Manual.