Re: Constants
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Sat, 25 Mar 2006 11:58:47 -0600
Craig wrote:
How are simple and typed constants stored? Do simple constants use memory?
Nope. To demonstrate, try asking for the address of one with the @ operator.
Resourcestrings look like constants when you declare them, but they aren't. Their values are always resolved at run time according to the current string values stored in the module's string table. Asking for the address of a resourcestring gets you a value of type PResStringRec. (The compiler thinks it's of type PString, which is wrong; you'll have to type-cast to get the right thing.)
What about string constants, how are they stored?
They're stored just like global variables. The only difference is that the compiler might not allow you to modifies them. You won't be able to use the assignment operator one them, and you can't pass them as "var" or "out" parameters, but you could get a pointer to one and then modify the constant via the pointer.
Typed constants aren't really constants. Notice that you can't use them in constant expressions:
const
Size: Integer = 2;
Data: array[0..Size - 1] of string = (
'Item 1', 'Item 2';
);
Combo = Data[0] + Data[1];
The Data declaration doesn't compile because the array bounds need to be constant and Size isn't really constant. The Combo declaration doesn't compiler because the Data array isn't constant. Those are two completely independent problems; Combo wouldn't compile even if Size were really constant.
Note that it's not possible to declare true array and record constants. To declare them, you need to include a type, which immediately makes them typed constants.
The Delphi books that I read have different answers on how constants are stored, and I don't know which to believe.
In part, that may be because you shouldn't rely on any particular implementation anyway. Caveat emptor. The description I gave above is accurate for the Win32 personality of Delphi 2005. The .Net personality almost certainly works differently, at least for typed constants. With Borland making changes to how the compiler works for new features, I wouldn't be surprised if implementations of older features changed along the way.
--
Rob
.
- References:
- Constants
- From: Craig
- Constants
- Prev by Date: Re: Constants
- Next by Date: No Beep!
- Previous by thread: Re: Constants
- Next by thread: No Beep!
- Index(es):
Relevant Pages
|