Re: Constants



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
.



Relevant Pages

  • Re: BASIC problem calling LIB$ RTL
    ... (STRING BY DESC, & ... using this prototype wouldn't have detected the problem. ... RENAME_ERROR was declared as a long function, so the compiler ... The problem is there is no way in BASIC to declare ...
    (comp.os.vms)
  • Re: advice on package design
    ... > for following scope. ... the compiler would have to do ... the extra verbiage required just to declare X. ... Unfortunately this is also untrue in Ada. ...
    (comp.lang.ada)
  • Re: Properties
    ... Is there any reason why this would not work and not simplify the ... Why should I have to declare any variable most of the time? ... The reason you explicitly declare fields used by a property is that the ... compiler needs to know what the code in the property does. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: use of erf intrinsic
    ... while it is also legal to declare a referenced function ... within the calling functionor even block, a la classic FORTRAN, ... The implementation (compiler) is required to check that references ... If you use K&R1 syntax, the compiler is not required to detect ...
    (comp.lang.fortran)
  • Re: Primitive curiosity...
    ... those types after the compiler has already used the information. ... "dispatch table" or something like that. ... contains the addresses of the primitive subprograms of the type. ... When you declare a type extension, ...
    (comp.lang.ada)