The following code illustrates the data overwrite problem when using pointers to store data in a byte array. Beginning at about the 57600 byte, additional data seems to overwrite the first part of the array. There are no comments; however, I've tried to use variable names that are fairly descriptive. This code creates a random terrain cross-section, then displays the data in screen mode 13. To view the problem, increase the value of NumImages.

DEFINT A - Z

DIM Posn AS DWORD

DIM ScnAdr AS DWORD

DIM ScnPtr AS BYTE PTR

DIM GndAdr AS DWORD

DIM GndPtr AS BYTE PTR

DIM HUGE Gnd(MAX, 1) AS BYTE

DIM Colour AS BYTE

DIM Kolor AS BYTE

DIM BytesPerImage AS WORD

DIM Counter AS WORD

DIM GndCounter AS BYTE

DIM Rad AS SHARED SINGLE

DIM Deg AS SHARED SINGLE

Pi! = 4 * ATN(1)

Deg = 180 / Pi!

Rad = Pi! / 180

GndWide = 320

GndHigh = 36

Colour = 8

NumImages = 4

BytesPerImage = GndWide * GndHigh

TotalBytes& = BytesPerImage * NumImages + NumImages

PRINT TotalBytes&

REDIM Gnd(TotalBytes&)

RANDOMIZE TIMER

GndCounter = 0

ScnAdr = &HA000 * 65536

Ly = RND(0, 9)

DO

Counter = 0

Lx = 0

X = 7

Posn = GndCounter * BytesPerImage

GndAdr = VARPTR32(Gnd(Posn))

DO

Elvn = RND(0, 9)

Y = RND(0, Elvn)

Azi# = Az(Lx, Ly, X, Y) * Rad

Dist = SQR((Lx - X) ^ 2 + (Ly - Y) ^ 2)

FOR I = 0 TO DIST

XX = Lx + I * SIN(Azi#)

YY = Ly - I * COS(Azi#)

FOR J = YY TO GndHigh - 1

Posn = J * 320 + XX

GndPtr = GndAdr + Posn

@GndPtr = Colour

NEXT J

NEXT I

Lx = X

Ly = Y

X = Lx + 8

Counter = X

IF X > 319 THEN X = 319

LOOP UNTIL Counter > 319

INCR GndCounter

INCR Colour

LOOP UNTIL GndCounter > NumImages - 1

! mov AX, &H13

! int &H10

Y = 0

GndCounter = 0

DO

Posn = GndCounter * BytesPerImage

GndAdr = VARPTR32(Gnd(Posn))

GndPtr = GndAdr

FOR I = 0 TO GndHigh - 1

Posn = I * 320

ScnPtr = ScnAdr + Posn

FOR J = 0 TO GndWide - 1

Kolor = @GndPtr

@ScnPtr = Kolor

INCR ScnPtr

INCR GndPtr

NEXT J

NEXT I

INCR GndCounter

SLEEP 3

LOOP UNTIL GndCounter > NumImages - 1

END

FUNCTION Az# (BYVAL X1#, BYVAL Y1#, BYVAL X2#, BYVAL Y2#)

X = X2# - X1#

Y = Y2# - Y1#

SELECT CASE Y

CASE < 0

Az# = -ATN(X / Y) * Deg!

IF X < 0 THEN Az# = 360 - ATN(X / Y) * Deg!

CASE > 0

Az# = 180 - ATN(X / Y) * Deg!

CASE 0

IF X > 0 THEN Az# = 90

IF X < 0 THEN Az# = 270

END SELECT

END FUNCTION

DEFINT A - Z

DIM Posn AS DWORD

DIM ScnAdr AS DWORD

DIM ScnPtr AS BYTE PTR

DIM GndAdr AS DWORD

DIM GndPtr AS BYTE PTR

DIM HUGE Gnd(MAX, 1) AS BYTE

DIM Colour AS BYTE

DIM Kolor AS BYTE

DIM BytesPerImage AS WORD

DIM Counter AS WORD

DIM GndCounter AS BYTE

DIM Rad AS SHARED SINGLE

DIM Deg AS SHARED SINGLE

Pi! = 4 * ATN(1)

Deg = 180 / Pi!

Rad = Pi! / 180

GndWide = 320

GndHigh = 36

Colour = 8

NumImages = 4

BytesPerImage = GndWide * GndHigh

TotalBytes& = BytesPerImage * NumImages + NumImages

PRINT TotalBytes&

REDIM Gnd(TotalBytes&)

RANDOMIZE TIMER

GndCounter = 0

ScnAdr = &HA000 * 65536

Ly = RND(0, 9)

DO

Counter = 0

Lx = 0

X = 7

Posn = GndCounter * BytesPerImage

GndAdr = VARPTR32(Gnd(Posn))

DO

Elvn = RND(0, 9)

Y = RND(0, Elvn)

Azi# = Az(Lx, Ly, X, Y) * Rad

Dist = SQR((Lx - X) ^ 2 + (Ly - Y) ^ 2)

FOR I = 0 TO DIST

XX = Lx + I * SIN(Azi#)

YY = Ly - I * COS(Azi#)

FOR J = YY TO GndHigh - 1

Posn = J * 320 + XX

GndPtr = GndAdr + Posn

@GndPtr = Colour

NEXT J

NEXT I

Lx = X

Ly = Y

X = Lx + 8

Counter = X

IF X > 319 THEN X = 319

LOOP UNTIL Counter > 319

INCR GndCounter

INCR Colour

LOOP UNTIL GndCounter > NumImages - 1

! mov AX, &H13

! int &H10

Y = 0

GndCounter = 0

DO

Posn = GndCounter * BytesPerImage

GndAdr = VARPTR32(Gnd(Posn))

GndPtr = GndAdr

FOR I = 0 TO GndHigh - 1

Posn = I * 320

ScnPtr = ScnAdr + Posn

FOR J = 0 TO GndWide - 1

Kolor = @GndPtr

@ScnPtr = Kolor

INCR ScnPtr

INCR GndPtr

NEXT J

NEXT I

INCR GndCounter

SLEEP 3

LOOP UNTIL GndCounter > NumImages - 1

END

FUNCTION Az# (BYVAL X1#, BYVAL Y1#, BYVAL X2#, BYVAL Y2#)

X = X2# - X1#

Y = Y2# - Y1#

SELECT CASE Y

CASE < 0

Az# = -ATN(X / Y) * Deg!

IF X < 0 THEN Az# = 360 - ATN(X / Y) * Deg!

CASE > 0

Az# = 180 - ATN(X / Y) * Deg!

CASE 0

IF X > 0 THEN Az# = 90

IF X < 0 THEN Az# = 270

END SELECT

END FUNCTION

## Comment