Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
strings initialized?
#1
Yesterday I experienced a kind of coding poltergeist with the following simple function:

Code:
function strings(times as ubyte, text as string ) as string

    dim i as ubyte
    dim result as string
    for i = 1 to times
        let result = result + text
    next i
    return result

end function

The program did strange things when printing the string returned by strings(): it printed other strings of the program that had nothing to do, and then froze. The reason was I had supposed the DIMed strings were initialized to an empty string but they are not; it took time to find out. When I added a simple line, everything worked as expected:

Code:
    dim result as string
    let result = ""

I've searched in the forum, but found no reference about this. The DIM wiki page doesn't mention string initialitazion. I just want to confirm this issue before updating the wiki. Are strings not initialized to empty strings because of a technical issue or simply because it's not implemented yet? Is this definitive or will it change in the future?
Reply
#2
This might be a bug. Every uninitalized string must default to "" or NULL (they're almost equivalent for the compiler).
I will test this and if so, move this topic to the Bug-Reports folder. Thanks! Wink
Reply
#3
Huh. We've seen this issue before, actually. I thought this bug was killed - did some code revert?
Reply
#4
britlion Wrote:Huh. We've seen this issue before, actually. I thought this bug was killed - did some code revert?
This is exactly what I was thinking. Anyway, everything is in the svn history :wink:
Reply
#5
I can't reproduce the bug. What version do you have?? Please, download latest one from the Archive and retry. You might be using an "old" (3 weeks old) version Tongue
Reply
#6
boriel Wrote:What version do you have?? Please, download latest one

I had dowloaded 1.2.6r1603c some days ago, but I've just realized I hadn't updated the symbolic link to it!, so I was still using 1.2.6r1603b. Anyway I tried both, and the program crashes with both.

I have just downloaded the last development version in the archive (1.2.6r1603e) and the program frozes after printing a different string, in fact a whole string array (that happened some hours ago with 1.2.6r1603b).

The problematic function (the same I mentioned before) is:

Code:
function strings(times as ubyte, text as string ) as string

    dim i as ubyte
    dim result as string
    let result="" ' NEEDED, OR BOOM :)
    for i = 1 to times
        let result = result + text
    next i
    return result

end function

And the sub that calls it is the following:

Code:
sub wcls()

    ' Clear the text window.

    ' 2010-06-17 First draft.

    dim l as ubyte
    for l=lastLine to firstLine step -1
        print at l,firstCol;strings(winWidth," ")
        ' print at l,firstCol;"                              " ' winWidth spaces, TEMPORARY ALTERNATIVE, anyway a bit faster
    next l
    home()

end sub


And:

Code:
const firstCol as ubyte = 1
const lastCol as ubyte = 30
#define winWidth lastCol-firstCol+1

Don't worry, it's not important.It will be solved by time Smile
Reply
#7
Again, your code is working OK for me :!: :?:

Two observations to your code:
  1. Values for firstLine and lastLine (uninitialized) are 0 by default.
  2. These variables have uByte type by default (got 2 warnings).

Since your FOR loop in wcls has a *negative* STEP and lower limit is 0, this loop will run forever due to UByte overflow. The FOR l iterator needs to get a value below 0 (negative) to stop.

Declaring variable l (the iterator) as Byte (signed) will solve this issue. Also, you have to define firstLine, lastLine (maybe as wcls parameters?). I used 1 and 24 respectively and it worked.

Try this and tell me.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)