[fpc-pascal]Two questions on initialisation of variables
Thomas Schatzl
tom_at_work at yline.com
Wed Mar 20 21:08:22 CET 2002
Subject: [fpc-pascal]Two questions on initialisation of variables
>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,
Yes.
>or for the contrary, compiler has optimisations that initialise the data
>segment with the appropriate values, saving valuable CPU clocks.
I doubt you'd notice the difference except you'd use ten-thousands of
such statements. It can be used for global variables only too and it's
only one time expense which makes it even more negligible.
>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 can use
VAR v : type = value;
for global variables as well.
>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.
This only works when you assign a constant string to the pchar.
>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);
You should use the methods from the strings unit as mentioned in the
docs... otoh stralloc() for example just calls getmem()...
I recommend using ansistrings instead of pchars.
Regards,
Thomas
More information about the fpc-pascal
mailing list