Thomas Schatzl wrote 30 lines
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.
Well, I am assigning strings too... (not too long strings though)
Show quoted text 12 lines
>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.
hmmm... I suppose I can use Delphi mode for Go32, can't I?
I have recompiled it with -Sd, and I get lots of new bugs, all of them being of the form:
Error: incompatible type for arg nº 1: Got <unkonwn type>, expected THIS or THAT...
I'll have to check that... (I am using a quite old version of the compiler yet, so this may be an old bug!)
Will Delphi mode create DJGPP-linkable code, even if I don't use classes and exceptions?
Show quoted text 16 lines
>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.
I have read about AnsiStrings, and it seems reasonable to me. I am using PChars just for two reasons: to be able to link with DJGPP and to be able to trigger direct DOS system calls that require pointers to null-terminated strings, and AnsiStrings, seem to fit well...
I suppose I can't use the + operator to concatenate AnsiStrings, can I?
Well, thanks for the advice!
Aitor
PD: Thanks to Peter too!