[alt.msdos.programmer] huge-far pointer and Turbo C++ V1

stephenj@deblil.Eng.Sun.COM (Stephen Johnson) (02/19/91)

/* The following program should be compiled with:

tcc -mh <prog>.c

This program allocates a very large array of memory
and addresses using the "far" key word.  Unfortunately, the
address calculations rap around at 64k.  Does anyone know why
this happens?  Is this a bug in the Turbo C++ libraries?

I am using Trubo C++ V1.0 on a Dell 310 (386 box)
with 640k of DOS memory and 4 Meg of real memory.
thanks, Stephen Johnson
*/

#include <stdio.h>
#include <alloc.h>

#define ULONG unsigned long

ULONG far *zbuffer = NULL;

void z_buffer_init();
void z_buffer_test();

main()
{
    printf("Available memory: %lu\n", farcoreleft());
    printf("We will allocate: %lu\n", (ULONG)sizeof(ULONG) *
        (long)320 * (long)200);

    zbuffer = (ULONG far *)farmalloc((ULONG)sizeof(ULONG) *
        (ULONG)320 * (ULONG)200);

    z_buffer_init();
    z_buffer_test();
}

void
z_buffer_init()
{
    ULONG far 	*zbuf;
    long	nwords = (long)320 * (long)200;

    zbuf = zbuffer;
    while (--nwords) {
        *zbuf = (ULONG)(((ULONG)1 << (ULONG)24) - (ULONG)512);
        zbuf++;
    }
}

void
z_buffer_test()
{
    long	 nwords = (long)320 * (long)200;
    ULONG far    *zbuf;

    zbuf = zbuffer;

    while (--nwords) {
        if (*zbuf > (ULONG)10) {
            *zbuf = (ULONG)10;
	}
	else {
	    printf("nwords: %lu\n", nwords);
	    printf("value: %lx\n", *zbuf);
	    printf("start: %lx\n", (ULONG)zbuffer);
	    printf("addr: failed at %lx\n", (ULONG)zbuf);
return;
	}
	zbuf++;
    }
}