Delphi's global variables and local variables

Source: Internet
Author: User
Tags define local

Global variables and local variables of Http://www.cnblogs.com/Stwo/archive/2011/07/11/2102816.htmlDELPHI

Global variables:

If we define a global variable in the area between the interface keyword and the implementation keyword in a cell of the application, if the unit is referenced elsewhere, the unit's full
The local variables can be accessed elsewhere, and of course we can define global variables after the implementation keyword in a cell of the application, but the global variables defined here can only be
The unit is accessed, meaning that it is private to the unit and cannot be accessed in any other cell. Global variables allocate memory in the application's data area, which exists in the executable module (EXE or DLL, etc.)
Within the file image, it is determined at the program compile time, until the application ends, and the memory address used by the global variable is fixed. A global non-pointer type that automatically allocates memory after declaring and initializes the value.

Global pointer type, which does not automatically allocate memory after declaration, and the value is nil.


Local variables:

We can define local variables in functions or methods (including procedures), and local variables are allocated on the stack of the application and are always allocated on the stack! The memory of a local variable is in a function or method (contains a procedure)
Allocated when called, and its memory is freed at the end of a function or method (containing procedure) call. Because the function or method (the containing procedure) is called each time, the top of the stack may change, so the local variable's memory
The address is a variable, local non-pointer type that automatically allocates memory after declaration, does not initialize the value, and the value is indeterminate (depending on the operation of the other program on the memory). Local pointer types, which do not automatically allocate memory after declaration,

But it randomly points to an address, so the address is not nil.

Note:

We do not advocate the use of global variables, and as far as possible with local variables, if we have to use global variables, we can put this global variable in the class (such as Tform) member area of the declaration or definition.

For local variables, we should be careful that it is not initialized as a global variable when it is defined, and it hides a huge risk when you are not paying attention to a program, so when you use local variables, you must first

Initialize, then use!

Delphi's global variables and local variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.