Every computer program uses memory for running (*). Memory is consumed by every variable in your program. It can be form, component, object, array, record, string or simple Integer. Memory can be allocated automatically for certain types of variables (such as Integer or static arrays), the other types require manual control of memory (for example, dynamic arrays). Essentially, from the point of operating system, each variable is characterized by its address (i.e. - location) and size.
Roughly speaking, program uses 3 “types” of memory: area for global variables, the stack and the heap.
Memory for global variables is allocated by OS loader when executable module is loading and it is freed when module is unloading. Global variables are those, which declared outside of class or any routine. The stack is used for allocating memory for local variables (which are declared in some function or procedure) and auxiliary data (such as return addresses or exception handlers). The heap is used for storing dynamic data.
Note, that for variables of dynamic types (such as dynamic arrays, strings, objects or components) - though the variable itself is stored in global area or stack, but its data is always allocated on the heap and it (often) require manual control.
Regardless of who allocates memory for the variable (you, manually or the compiler, automatically), memory for each variable must be allocated before its using, and later (when the variable is no longer needed) it should be freed.
Sometimes there can be a situation, where your application trying to get access to certain memory location, which wasn’t allocated or was already released - due to bugs in your code. When such things happens - the CPU raises an exception of class EAccessViolation. The usual text for this error is as follows: “Access violation at address XXX in module ‘YYY’. Write/read of address ZZZ”. Though there is the one simple reason for this kind of error, the real situations for it can be very different.
Tuesday, May 12, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment