views:

733

answers:

5

Delphi 2009 Win32.

The code below tries to add a 257 length string to a memo. It compiles and runs fine, but nothing is added to the memo.

Memo1.Lines.Add('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');

Looks like a compiler bug. Is it? Because if the string was 256 long, I'd get a compiler error and couldn't compile the app.

Any way to make the app break when the developer tries to do something like this?

I know I could split the string and make this code work, but my point is to prevent developers for using this invalid code without noticing.

Thanks

A: 

I don't know about D2009, but under Delphi 6 at least, string literals are limited to 255 characters, and the compiler diagnoses the error.

anon
I believe the 255 limit is due to Delphi (pre-D2009 anyway) storing the length in the first byte of the string buffer. I.e. string[0] is the length, and being a byte it can store 0-255.
WileCau
All versions of Delphi since Delphi 2 have supported long string variables and are not limited to 255 characters. Except, it seems, for string literals!
anon
A: 

In Delphi 2007, I get:

[DCC Error] Unit1.pas(29): E2056 String literals may have at most 255 elements

In Delphi 5, I get:

[Error] Unit1.pas(29): String literals may have at most 255 elements

If the D2009 behavior is as you describe, then two things come to mind:

1 - They expanded the limit on the # of chars in a string, but the TMemo can still only accept up to 255.

or

2 - It's a plain old bug

As far as preventing it, the only thing I can think of is to make a regex to search for these strings in your .PAS files.

JosephStyons
My 2009 gives the same error message.
Gamecat
In light of the answer from Cesar Romero... do you have the IDE Fix pack installed?
JosephStyons
I doubt that he has the 2.6 Beta 1 installed because this one is brand new and wasn't available to public until Cesar Romero posted the link here.
Andreas Hausladen
+13  A: 

This is a Delphi 2009 bug with string literals, it should raise the same error as D2007.

Try latest version of Andreas IDE Fix pack, its supose to fix this bug. http://andy.jgknet.de/blog/?page_id=246

Cesar Romero
And Andreas keep fixing Delphi over and over. :)
Erick Sasse
Yes, Andreas is dong a great job!
Edwin
+1  A: 

The string literal can be only 255 characters long. Not sure why they kept this limitation. But you can solve it using multiple literals:

Memo1.Lines.Add('i have 128 chars' + 'i also have 128 chars').
Gamecat
+1  A: 

I agree with Gamecat, however if your dealing with a string that large, I would break it into muliple lines to assist in reading/editing.

if you are LITTERALLY trying to create 257 "a"'s then why not use the DupeString function in the StrUtils unit?

Memo.Lines.Add( DupeString('a',257) );

Much easier to read, and maintain later. If you are doing this in a loop and therefore are worried about performance, then assign the function result to a local variable and use the variable.

var
  sLotsOfAs : string;
  ix : integer;
begin
  sLotsOfAs := DupeString('a',257);
  for ix := 0 to 1000000 do
    Memo.Lines.Add( sLotsOfAs );
end;
skamradt