April 30, 2024, 04:04:06 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Return an ISTRING from a Subroutine?

Started by tekrat, November 14, 2007, 11:43:49 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

tekrat

The program crash error when I try to return an ISTRING from by subroutine.

SUB MakeBug(), STRING

DEF RETURN_VALUE[2000]:ISTRING
FOR X = 1 to 1999
RETURN_VALUE = APPEND$(RETURN_VALUE, "*")
NEXT X
RETURN  RETURN_VALUE

ENDSUB

OPENCONSOLE

JUNK = MakeBug()
PRINT JUNK

PRINT "Press Q to quit"
DO: UNTIL INKEY$ = "Q"
CLOSECONSOLE


I've tried to making my subroutine and ISTRING but of course that errors at compilation.  Any ideas?

SUB MakeBug(), ISTRING

Ionic Wind Support Team

You are using autodefinition of the string JUNK.   Which means since you didn't define it your self the compile makes it a STRING type whose length is 255 characters.  And returning a 2000 character string will definately overwrite it and another 1745 bytes of memory your program doesn't own ;)

So the answer is to define your variables


SUB MakeBug(), STRING

DEF RETURN_VALUE[2000]:ISTRING
FOR X = 1 to 1999
RETURN_VALUE = APPEND$(RETURN_VALUE, "*")
NEXT X
RETURN  RETURN_VALUE

ENDSUB

OPENCONSOLE

DEF JUNK[2000] as ISTRING
JUNK = MakeBug()
PRINT JUNK

PRINT "Press Q to quit"
DO: UNTIL INKEY$ = "Q"
CLOSECONSOLE

Ionic Wind Support Team