[fpc-pascal]Two questions on initialisation of variables
Aitor Santamaria Merino
aitor.sm at wanadoo.es
Wed Mar 20 23:03:16 CET 2002
Thomas Schatzl wrote:
> Subject: [fpc-pascal]Two questions on initialisation of variables
>>Many of my programs start like this:
>> a, b,...: byte;
>> 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.
> 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)
>>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
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 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!
PD: Thanks to Peter too!
More information about the fpc-pascal