Strings - how many characters are they assigned?

New to FreeBASIC? Post your questions here.
xbgtc
Posts: 157
Joined: Oct 14, 2007 5:40
Location: Australia

Strings - how many characters are they assigned?

Postby xbgtc » Jan 08, 2013 19:04

I got this error "string too long - truncated" when assigning a 1500 character string to a variable but seems only when previous data is of a small length eg. i can do DIM AS STRING csd(90) and load 1500 character strings from hard disk into it no problem but when i try to load same data into another array cty(10,40) in which elements 1,1-39 are filled with 3 and up to 12 character strings, i get the above error when it tries to store the 1500 character string into position 1,40 - any ideas what's wrong?
gothon
Posts: 223
Joined: Apr 11, 2011 22:22

Re: Strings - how many characters are they assigned?

Postby gothon » Jan 08, 2013 19:19

Is it possible you are using fixed length strings? (ie. string * n)
Variable length strings are dynamically allocated and can be as large as the available memory in the 32 bit address space. (over 2 billion characters)

The following program I made produces no error.

Code: Select all

Dim As String cty(10,40)

For I As Integer = 1 To 39
    cty(1,I) = String(12, "A")
Next I
cty(1,40) = String(1500, "!")
Print cty(1,40)

Sleep


Can you post a short test program that reproduces the problem?
xbgtc
Posts: 157
Joined: Oct 14, 2007 5:40
Location: Australia

Re: Strings - how many characters are they assigned?

Postby xbgtc » Jan 08, 2013 19:44

ahh yer no worries and your proggy works here too.
you see the big string below and error message is "literal string too big, truncated" but it does compile ok just i have lost all data in the string after "Beezel" (in 9th line from bottom)

Code: Select all

dim as string country(1,41) '10 down 41 accross

for i as integer=1 to 1 'arrays down first then accross
    for j as integer=1 to 41
        read country(i,j) 'make data all Aus first then the rest eg. 1,1-1,33 then 2,1-2,33
    next j
next i

for i as integer = 1 to 1
    for j as integer=1 to 41
        ? country(i,j)
    next j
next i
sleep

'X(accros) Y(down)
'                   Telco    CTY   EXIT    STATE      STATE
'    Country       X     Y   code  code    X    Y    numbers
data "Australia","790","298","0011 ","780","734","778","814","750","800","778","800","804" 'X
data "296","284","240","274","270","286","266","260","290" 'Y
data "61 8 8332 7642","61 8 9284 6422","61 8 8943 5874","61 7 4688 1963"
data "61 8 9022 7614","61 2 6882 2399","61 8 8952 3069","61 7 4724 4281","61 2 6230 4752"
data "0~0~25~bbs.dat~10~Teddieboy~ted1961~ZyxelU-90E~Greetings Sysop!\\~0~0~0~1~2~3~1~2~3~0~0~5~5~0~0~b~\New users only have limited access!\~           Welcome to WOMBAT BBS\-==========================================-\2 lines on modem Zyxel U-90E IP-Link 1mbit/s\****** System Operator: Bazza Holmes *******\--------------------------------------------\Tornado+/Dos 1.72[ASC]alpha/10-Mar-74[17:17]\\Enter Name: ~Password:   ~\1. Who's online\2. Join\3. Exit BBS\\~Main Menu\=========\\1. Games\2. Files \3. Quit\\~Astroboy,Knight,Mal,Zenice,~Currently not accepting new members!\~[1] Asteroids\[2] Beach Heada\[3] Chessex\[4] Galaxians\[5] Wolfenstein\\[B]ack  [SPACE] Selects  [ENTER] Downloads\\~[1] arj.exe\[2] battoexe.lzh\[3] disccopy.zip\[4] ice.exe\[5] lz.exe\\[B]ack  [SPACE] Selects  [ENTER] Downloads\\~0~0~0~\\Incorrect Password!\<Connection Terminated>~3~SYSOP MAIN MENU\_______________\\1. USERS\2. BANK\3. FILES\4. EXIT\\SELECT ~1~2~3~4~0~6~d~3~0~01 Astroboy    1        1        1        1        1\02 Beezel      0        1        0        1        0\03 Knight      1        0        1        1        1\04 Mal         1        1        1        1        1\05 Teddieboy   1        1        1        1        1\06 Zenice      1        1        1        1        1\            Enabled TimeLimit  Mail    Upload   Download\\[TAB] Selects  [ENTER] Toggles  [B]ack~$21.00cr       \\[B]ack  [D]eposit  [W]ithdraw\\~bbs.dat\users.dat\bbs.cfg\\[B]ack\\~0~16~9~4~111110101011111111111111110111~4~\\Exiting system...||\\~Bye!\\~123~123~1234~5~"
data "2","3","4","5","6","7","8","9","00000"
gothon
Posts: 223
Joined: Apr 11, 2011 22:22

Re: Strings - how many characters are they assigned?

Postby gothon » Jan 08, 2013 19:53

It would seem there is a maximum length to a string literal. Eg a single block of characters between "" in the source file. You should probably break the larger string up into multiple pieces and concatenate the pieces back together to rebuild the larger string using &.
xbgtc
Posts: 157
Joined: Oct 14, 2007 5:40
Location: Australia

Re: Strings - how many characters are they assigned?

Postby xbgtc » Jan 08, 2013 20:02

ahh yeh as when that string is stored in the text file it has no quotes and i have no trouble loading it into an array.

what is "&" and how do i use it. Dunno if i can split/join the data actually.

Another question - i have 90 of those long strings which i only use 1 at a time in my game. Is it better to load off HD or load into array at startup as it was previously?
fxm
Posts: 9012
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Strings - how many characters are they assigned?

Postby fxm » Jan 08, 2013 20:42

A literal string is truncated from 1024 characters.
gothon
Posts: 223
Joined: Apr 11, 2011 22:22

Re: Strings - how many characters are they assigned?

Postby gothon » Jan 08, 2013 20:43

& is the string concatenation operator. You can also use + to concatenate strings although I avoid it because it is often ambiguous weather you are adding numbers or concatenating strings.
http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgOpConcatConvert
http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgOpConcat

It will probably be easier to load the data off the hard disk although there are some techniques you can try to fit them in your source file better if you choose to do so. Your string looks like it uses "\" and "\\" as delimiters. If you break the string into separate literals wherever there are "\" , that would be a natural place to do so. You may not even need to recombine the string depending on how your program works.

Furthermore you don't have to do this manually. You could write a program that reads the string from a file, then writes to another file what the source code would be with the string broken apart into separate literals. You can then copy the generated source code into your real source code.
xbgtc
Posts: 157
Joined: Oct 14, 2007 5:40
Location: Australia

Re: Strings - how many characters are they assigned?

Postby xbgtc » Jan 08, 2013 21:08

Thanks guys as knowing the 1024 limit for literal strings (string in quotes) is at fault i have achieved exactly what i wanted to to in the first place by creating the data in a text file with print # (so data not in quotes) and loading it into the same array first then fill the rest of the array via read statements then encyrpt and save the lot as .dat files - cheers.
TJF
Posts: 3473
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Strings - how many characters are they assigned?

Postby TJF » Jan 09, 2013 9:30

fxm wrote:A literal string is truncated from 1024 characters.

That's not correct! You can have up to 1024 characters in a line, but you can concatenate several lines in to one STRING like

Code: Select all

VAR a = "123456789" _
        "123456789"


and make the STRING bigger than 1024 characters that way.
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Re: Strings - how many characters are they assigned?

Postby 1000101 » Jan 10, 2013 2:42

I didn't know about the 1k literal string limit but for reference dynamic strings (non-fixed length strings) have a limit of either 2GB, 3GB or 4GB (win32, win32 with LAE* enabled in the executable header and win32 with LAE under wow64). This is simply due to the addressing limits of 32-bit programs. I'm not sure about linux or dos but I suspect the limits are similar (although most dos extenders only recognize up to 3.5GB even if there is more).


*LAE is Large Address Aware - Meaning that your program has been written with the ability to properly handle unsigned 32-bit pointers. Without this flag in the header windows will never give you a pointer which would grow beyond the 2GB border as it is unable to know if you are using signed or unsigned pointers. ie, if you allocate 512MB the largest pointer which would be returned would be &H5FFFFFF (1.5GB-1), anything higher than that could cause sign errors in pointer math. If there is not enough memory available below the limit then the allocation will fail.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 3 guests