Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-03

Two questions on initialisation of variables

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Aitor Santamaria Merino

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?

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.

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);

Aitor

Thomas Schatzl
to Aitor Santamaria Merino

Subject: [fpc-pascal]Two questions on initialisation of variables

Show quoted text 14 lines
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.

Show quoted text 5 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.

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

Peter Vreman
to Aitor Santamaria Merino: Many of my programs start like this: var a, b,...: byte; ... begin a:= 5; b:= 7; ... So I…
you wrote 21 lines
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

Aitor Santamaria Merino
to Thomas Schatzl: Subject: [fpc-pascal]Two questions on initialisation of variables Yes. I doubt you'd…
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!

Peter Vreman
Show quoted text 6 lines
> 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?

Delphi mode is independent of the target OS. It are language features

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

THI> S

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!)

Try using -S2 that will turn on the delphi features but keep the fpc style of checking for things like procedure variables

Will Delphi mode create DJGPP-linkable code, even if I don't use classes and exceptions?

See first answer

Show quoted text 23 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?

You can use all the normal string operations you have also for shortstrings (normal pascal strings)