[fpc-pascal]Two questions on initialisation of variables

Peter Vreman peter at freepascal.org
Wed Mar 20 21:23:25 CET 2002


At 19:27 20-3-2002, you wrote:
>Hi,
>
>Many of my programs start like this:
>
>var
>   a, b,...: byte;
>...
>
>begin
>     a:= 5;
>     b:= 7;
>...
>
>So I was wondering if these sentences are translated into MOV sentences, 
>or for the contrary, compiler has optimisations that initialise the data 
>segment with the appropriate values, saving valuable CPU clocks.
>If the answer is no, is there a way to do this, other than
>
>CONST v: type = value;
>
>that seems to me "inelegant" for Pascal?

In delphi mode you also can have VAR v:type=value


>And the second is for initialisation of PChars. The examples of reference 
>guide, pag 29, seem to suggest that I don't need to New() pchars. Is that 
>right? I suppose this is only at initialisaton time.

You can do pchar:='constant string'. The pchar will then point into the 
datasegment with read-only data, so changing the value can then result in a 
sigsegv when the OS supports rodata segments


>Suppose that for some reason I had to new a PChar, I suppose that New() 
>would only allocate 1 byte, so I'd have to use GetMem, am I right??
>GetMem (p, maxsize+1);

Yes, you have to use Getmem()


Peter





More information about the fpc-pascal mailing list