Sign in Join

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

Mailing lists / fpc-devel / 2000-09

array of char

11 messages · 9 people · –

Reply by email Subscribe to this list Your subscription

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

Konkoly Tamas

I found an error.
Source:

const
CharsDes1:array[0..383]of char=
'0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'+ '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'+ '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'+ '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'+ '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'+

  '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF';
var rr,rt:longint;
begin
 for rr:=0 to 383 do begin
  write(CharsDes1[rr]);
 end;
end.

The program just writes the first 255 characters and the other characters instead of write zero codes.

I think it's a bug because the 'string' isn't equal 'array[0..x]of char'.

.,--/'{[< Tcc of Inquisition >]}'\--,.

Reply by email

R Wong
Show quoted text 14 lines
const CharsDes1:array[0..383]of char= '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'+ (snipped 4 identical lines...) '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'; var rr,rt:longint; begin for rr:=0 to 383 do write(CharsDes1[rr]); end. The program just writes the first 255 characters and the other characters instead of write zero codes. I think it's a bug because the 'string' isn't equal 'array[0..x]of char'.

The problem is that, when you Assign something to a var-constant (initialized variable array),
the expression (your long string) will get processed first, before passing along to the const/var.
Thus, the shortstring Concatenation operation truncated your string before it is passed to the const.

It is perfectly fine with your data structure. The problem is the way that it is assigned.
(In other words, you cannot initialize that array directly)

This problem dates back to a similar TP7 problem, where const BigNum=1000000000000000;
will cause an error, because TP7 assumes any number without decimal to be an integer.
(This does not happen on FPC, because it is clever enough to do implicit typecast on constants as well)

Any solutions (tricks, hacks, ...) ?

Reply by email

Jonas Maebe
Show quoted text 6 lines
> const > CharsDes1:array[0..383]of char= > '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'+ > (snipped 4 identical lines...) > '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'; >

[cut]

Show quoted text 6 lines
The problem is that, when you Assign something to a var-constant (initialized variable array), the expression (your long string) will get processed first, before passing along to the const/var. Thus, the shortstring Concatenation operation truncated your string before it is passed to the const.

This is correct.

It is perfectly fine with your data structure. The problem is the way that it is assigned. (In other words, you cannot initialize that array directly)

Yes you can, but it's a lot more typing work:

const
CharsDes1:array[0..383]of char=
('0','1','2','3','4',...)

Jonas

Reply by email

Pápai András
Show quoted text 6 lines
I found an error. Source: const CharsDes1:array[0..383]of char= '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'+

[...]

Show quoted text 6 lines
The program just writes the first 255 characters and the other characters instead of write zero codes. I think it's a bug because the 'string' isn't equal 'array[0..x]of char'.

This works with PChar type:

const
CharsDes1:PChar=
'0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'+

Andras

Reply by email

Florian Klaempfl
to Jonas Maebe: [cut] This is correct. Yes you can, but it's a lot more typing work: const…
>Thus, the shortstring Concatenation operation truncated your string before >it is passed to the const. This is correct.

No, it isn't. The strings are concatened as ansistrings but when writing the constant, the string is cut off to 255 chars, see ptconst.pas
around line 540:

                   p:=comp_expr(true);
                   do_firstpass(p);
                   if p^.treetype=stringconstn then
                    begin
                      if p^.length>255 then
                       len:=255
                             ^--- should we fix this?
                      else
                       len:=p^.length;
                      s[0]:=chr(len);
                      move(p^.value_str^,s[1],len);
                    end

Reply by email

Michael Van Canneyt
to Florian Klaempfl
Florian Klaempfl wrote 23 lines
Jonas Maebe schrieb: > >Thus, the shortstring Concatenation operation truncated your string before > >it is passed to the const. > > This is correct. No, it isn't. The strings are concatened as ansistrings but when writing the constant, the string is cut off to 255 chars, see ptconst.pas around line 540: p:=comp_expr(true); do_firstpass(p); if p^.treetype=stringconstn then begin if p^.length>255 then len:=255 ^--- should we fix this? else len:=p^.length; s[0]:=chr(len); move(p^.value_str^,s[1],len); end

It depends on the type of the const:
- For shortstring it should be cut.
- For pchar not (obvious).
- for array of char not.
- for ansistring don't cut either.
Is it possible to determine the type here ?

Michael.

Reply by email

Jonas Maebe
Show quoted text 6 lines
> >Thus, the shortstring Concatenation operation truncated your string before > >it is passed to the const. > > This is correct. No, it isn't. The strings are concatened as ansistrings but

Make that pchars :)

Jonas

Reply by email

Florian Klaempfl
to Michael Van Canneyt: It depends on the type of the const: - For shortstring it should be cut. - For pchar not…
> No, it isn't. The strings are concatened as ansistrings but > when writing the constant, the string is cut off to 255 chars, see > ptconst.pas > around line 540:

...

Show quoted text 6 lines
It depends on the type of the const: - For shortstring it should be cut. - For pchar not (obvious). - for array of char not. - for ansistring don't cut either. Is it possible to determine the type here ?

It is know that it is an array of char .

Reply by email

startx
to R Wong: The problem is that, when you Assign something to a var-constant the expression (your…
R Wong wrote 7 lines
> const > CharsDes1:array[0..383]of char= > '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'+ > (snipped 4 identical lines...) > '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'; Any solutions (tricks, hacks, ...) ?

A stupid and dirty solution: use more arrays, as many as you need, declared one after the other, and then use the first array with the index going outside boundaries. I did'nt test this, but should work - only pay attention at the alignment (word or longword). I've seen the Pchar solution, and seems better.

_________________________________________________________ Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

Reply by email

Marco van de Voort
to Jonas Maebe: [cut] This is correct. Yes you can, but it's a lot more typing work: const…
Show quoted text 10 lines
>It is perfectly fine with your data structure. The problem is the way that >it is assigned. >(In other words, you cannot initialize that array directly) Yes you can, but it's a lot more typing work: const CharsDes1:array[0..383]of char= ('0','1','2','3','4',...)

Check Data2inc in the utils directory. IIRC it could change textfiles to array of char output like above.

Marco van de Voort ( or )

Reply by email

Peter Vreman
to Florian Klaempfl: No, it isn't. The strings are concatened as ansistrings but when writing the constant,…
you wrote 23 lines
Jonas Maebe schrieb: > >Thus, the shortstring Concatenation operation truncated your string before > >it is passed to the const. > > This is correct. No, it isn't. The strings are concatened as ansistrings but when writing the constant, the string is cut off to 255 chars, see ptconst.pas around line 540: p:=comp_expr(true); do_firstpass(p); if p^.treetype=stringconstn then begin if p^.length>255 then len:=255 ^--- should we fix this? else len:=p^.length; s[0]:=chr(len); move(p^.value_str^,s[1],len); end

Fixed. Only for tp mode the length is now limited to 255. Also for pchars the length is limited to 255 in tp mode.

Peter

Reply by email