### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: I need a 512 character string array  (Read 622 times)

#### Nera

• Jr. Member
• Posts: 79
##### I need a 512 character string array
« on: April 09, 2020, 08:52:46 pm »
Fila : array [0..255] of string[512];

Os possible?

Thanks

#### Warfley

• Full Member
• Posts: 120
##### Re: I need a 512 character string array
« Reply #1 on: April 09, 2020, 09:06:00 pm »
I think that string[XXX] is basically using ShortString[XXX] (correct me on that if I'm wrong), which uses the first Char to store the length (This is why Strings start at Index 1). Therefore the length of shortstrings is capped to 256 (1 Byte = 1 Char).

You could either use dynamic (Ansi)Strings with no hardcoded Length, or an Array[0..512] of Char

#### jamie

• Hero Member
• Posts: 2896
##### Re: I need a 512 character string array
« Reply #2 on: April 09, 2020, 09:21:42 pm »
That's possible but a large chunk of memory...

I don't know what you are doing in your app but you could use a TStringList for that.

in any case using a fixed array means that all strings will start at the exact offset regardless of length of each one

Var
Fila :Array[0..255] of array[0..512] of char;

Number 1 at blue screen app creations!

#### winni

• Hero Member
• Posts: 1341
##### Re: I need a 512 character string array
« Reply #3 on: April 09, 2020, 09:45:52 pm »
Hi!

array[0..255] of array[0..511] of char
is
2 * 2^16
is
131 072

131 kB - so dont panic!

Winni

#### lucamar

• Hero Member
• Posts: 2831
##### Re: I need a 512 character string array
« Reply #4 on: April 09, 2020, 09:48:19 pm »
That's possible but a large chunk of memory...

Not so large, is it? If it were possible, which it isn't, it would be just 256*513 bytes, about 128 KiB.

But it's not possible as such, since the declaration of String[SomeLenght] is meant for old-style "short" strings, which cap at 255 (\$FF) chars. One has to use some workaround, such as using array of char or (probably better) new-style ansistring.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

#### jamie

• Hero Member
• Posts: 2896
##### Re: I need a 512 character string array
« Reply #5 on: April 09, 2020, 10:15:01 pm »
I suppose If one wants to get fancy you could make a Type of LargeFixed Sized strings with helper functions and operator overloads. That way it would then look and behave like a normal string in code land...

Number 1 at blue screen app creations!

#### winni

• Hero Member
• Posts: 1341
##### Re: I need a 512 character string array
« Reply #6 on: April 09, 2020, 11:17:08 pm »
Hi!
Hmm - that does not look so easy.

If you want to  use the length of 511 char, then you need 9 bits for the length .
That does obvious not fit into the length byte.

Where to put that surplus bit?

Winni

#### winni

• Hero Member
• Posts: 1341
##### Re: I need a 512 character string array
« Reply #7 on: April 09, 2020, 11:22:33 pm »
Okay:

Code: Pascal  [Select][+][-]
1. Type
2. TLargeFixedSizedString = array of shortstring;

Grouse, grouse ....

Winni

#### Aidex

• New Member
• Posts: 40
##### Re: I need a 512 character string array
« Reply #8 on: April 10, 2020, 06:55:53 am »
I think
Array[0..255] of String;
or
Array of String;
is the solution for this, because String as long string can take the required length of 512 characters (and it's also flexible).

#### PascalDragon

• Hero Member
• Posts: 1488
• Compiler Developer
##### Re: I need a 512 character string array
« Reply #9 on: April 10, 2020, 10:29:38 am »
Fila : array [0..255] of string[512];

Os possible?

It's not possible as such, no. But if you'd tell as what you intend to achieve we would be able to point you to a correct solution for this.