🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.msdos.programmer
5 messages
5 total messages Started by Brad Hlista Fri, 27 Oct 1989 20:21
Turbo C far pointers
#229
Author: Brad Hlista
Date: Fri, 27 Oct 1989 20:21
24 lines
887 bytes
I am having a problem of getting an allocated block of far memory
to return float values.  Here is how variables are declared and dereferenced:

far *ptr;

main()
{
int i;

	ptr=(float *) farmalloc(100000);

	f(i=0;i<1000;i++)
		*(ptr+i)=3.1415927;

	for(i=0;i<10;i++)
		printf(" %f", (float) *(ptr+i) ) ;  /* is yielding 3.00000 */
}

Can someone please help me understand what is going on?
Thanks.

Brad
elrond@titan.tsd.arlut.utexas.edu
   
Re: Turbo C far pointers
#231
Author: Jim Wright
Date: Sat, 28 Oct 1989 19:08
36 lines
1367 bytes
In a recent posting elrond@titan.tsd.arlut.utexas.edu (Brad Hlista) writes:
| I am having a problem of getting an allocated block of far memory
| to return float values.  Here is how variables are declared and dereferenced:

| far *ptr;

I think this is the big part.  An int pointer is not the same as a float
pointer.  The following program works for me (MSC).

#include <stdio.h>
#include <malloc.h>
int main(void);

int main()
{
    int i;
    float far *start;
    float far *p;

    start = (float far *) _fmalloc(1000*sizeof(float));

    if (start != NULL) {
        printf("Good.\n");
        for (p=start,i=0 ; i<1000 ; i++)
            *(p++) = 3.1415927;
        for (p=start,i=0 ; i<10 ; i++)
            printf("%f\n", *(p++));
    }
    else
        printf("Oh shit.\n");
    return(0);
}

-- 
Jim Wright
jwright@atanasoff.cs.iastate.edu
Re: Turbo C far pointers
#233
Author: Bruce Becker
Date: Sun, 29 Oct 1989 01:42
50 lines
1860 bytes
In article <565@titan.tsd.arlut.utexas.edu> elrond@titan.tsd.arlut.utexas.edu (Brad Hlista) writes:
|I am having a problem of getting an allocated block of far memory
|to return float values.  Here is how variables are declared and dereferenced:
|
|far *ptr;
|
|main()
|{
|int i;
|
|	ptr=(float *) farmalloc(100000);
|
|	f(i=0;i<1000;i++)
|		*(ptr+i)=3.1415927;

Should be:	*((float *)(ptr+i)) = 3.1415927;

	Otherwise the "far" declaration of "ptr"
	will force a type conversion to the default
	of type "int". The way you have it the
	contents of of *(ptr+i)" contains an int
	value which is coerced back to float
	in the printf statement.

|	for(i=0;i<10;i++)
|		printf(" %f", (float) *(ptr+i) ) ;  /* is yielding 3.00000 */

Should be	printf(" %f", *((float *)(ptr+i)) ) ;  /* is yielding 3.1415927 */

|}
|
|Can someone please help me understand what is going on?
|Thanks.

	You need to ensure that either by default
	or by explicit declaration that
	sizeof(far) == sizeof(float); is there a
	"far long" type? Better yet, a "far float"?

	I didn't actually try this so if your compiler
	barfs on "*((float *)ptr)" where "ptr" is a
	"far *" declaration, well... you could try
	"far float * ptr;", etc...

Cheers,
-- 
  .::.	 Bruce Becker	Toronto, Ont.
w \@@/	 Internet: bdb@becker.UUCP, bruce@gpu.utcs.toronto.edu
 `/c/-e	 BitNet:   BECKER@HUMBER.BITNET
_/  \_	 In the future the term "wife" will have no gender significance
Re: Turbo C far pointers
#234
Author: Charles Hannum
Date: Sun, 29 Oct 1989 05:08
11 lines
786 bytes
Two suggestions:

  1)  Your main problem is that you need to use the declaration:

      far float *ptr;

      I won't even try to explain what happens otherwise.

  2)  You really should use "ptr[i]" rather than "*(ptr+i)".  It effectively
      does the same thing, but it makes for much cleaner and much more
      readable code.
Re: Turbo C far pointers
#238
Author: Tommy Levitte
Date: Tue, 31 Oct 1989 11:42
63 lines
2782 bytes
In article <990@becker.UUCP> bdb@becker.UUCP (Bruce Becker) writes:
Bruce> Xref: sics.se alt.msdos.programmer:153 comp.sys.ibm.pc:29919

Bruce> In article <565@titan.tsd.arlut.utexas.edu> elrond@titan.tsd.arlut.utexas.edu (Brad Hlista) writes:

Bruce> |far *ptr;

Bruce> |main()

[... unimportant things deleted ...]

Bruce> |	f(i=0;i<1000;i++)
Bruce> |		*(ptr+i)=3.1415927;

Bruce> Should be:	*((float *)(ptr+i)) = 3.1415927;

(* GAACK *) This means you should use the i'th INTEGER location, not float!
Wow. The effect when printing out would be quite a sight...

No, the correct syntax would be : *((float *)ptr + i)

This way, you tell the compiler that ptr really is a float pointer, then
offset the whole thing with i.

In your solution, the offset would be 2*i bytes (2 = sizeof(int)). In mine,
the offset would be 4*i bytes (4=sizeof(float)) !!!!

Bruce> |	for(i=0;i<10;i++)
Bruce> |		printf(" %f", (float) *(ptr+i) ) ;  /* is yielding 3.00000 */

Bruce> Should be	printf(" %f", *((float *)(ptr+i)) ) ;  /* is yielding 3.1415927 */

Likewise here. Use *((float)ptr + i) instead !

Bruce> 	sizeof(far) == sizeof(float); is there a
Bruce> 	"far long" type? Better yet, a "far float"?

Wow. sizeof(far) ! Never seen that before. This would mean 'far int', which is
not (as far as I know) permitted in TC.
far is only applied on pointer and functions, to tell the compiler you need a
4-byte adress to get those objects. Thus, far is a modifier, not a type.
Now, about int. int is the same as short in TC, a 2-byte integer. float is a
4-byte float.

One IMPORTANT rule when you program in C: NEVER assume anything about the size
of differnet types. NEVER !!!!!

Bruce> 	I didn't actually try this so if your compiler
Bruce> 	barfs on "*((float *)ptr)" where "ptr" is a
Bruce> 	"far *" declaration, well... you could try
Bruce> 	"far float * ptr;", etc...

Quite surprising. I just tried it with Turbo C v 2.0, writing 
*((float *)ptr+i), and it worked just nice. writing *((float *)(ptr+i)) gave
me 0.000000 when I tried it...


--
Tommy Levitte 
	gizmo@nada.kth.se
	gizmo@kicki.stacken.kth.se
	gizmo@ttt.kth.se
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads