home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!mcrware!adam
- From: adam@microware.com (Adam Goldberg)
- Subject: Re: Problem with Turbo-C: calculating with pointers
- Message-ID: <1993Jan8.161709.10524@microware.com>
- Sender: news@microware.com
- Nntp-Posting-Host: ren
- Organization: Microware Systems Corp., Des Moines, Iowa
- References: <1993Jan7.112219.578@reks.uia.ac.be>
- Date: Fri, 8 Jan 1993 16:17:09 GMT
- Lines: 59
-
- derijkp@reks.uia.ac.be (Peter De Rijk) writes:
-
- >I am porting a program (working on different platforms such as VAX and DEC) to
- >the PC. I am using Turbo C++ v.3.0. I compile using the huge memory model.
- >I seem to have tracked down the problem (well, one of the problems.) I don't
- >know what to do about it though. Here's a little program to illustrate the
- >problem:
-
- >#include <alloc.h>
-
- >main()
- >{
- >char *pointer,*begin,*end;
- >long size;
-
- >size=71800;
- >pointer=(char *)farmalloc(size*sizeof(char));
- >begin=pointer;
- >end=pointer+size;
- >printf("%p %p %ld",begin, end, end - begin);
- >}
-
- >I would expect end - begin to return 71800. However it seems to return 6264.
- >This is exactly 64K less.
- >If this happens for all pointer calculations, I'm in big trouble.
- >Any ideas to solve this.
-
- Huge model normalizes all pointers, which allows you to access a whole
- 64k area with one pointer (ie, huge_char_ptr[64000]).
-
- Here's what is happening:
-
- pointer=(char *)farmalloc(71800); /* Pointer gets assigned the value
- 'seg:0' (where seg is arbitrary).*/
- begin = pointer; /* Ok, no problem so far */
- end = pointer+size; /* Ok, sega:0 + 0:0x11878 BUT WAIT, 0x11878 is
- > 0xffff, therefore the actual calculation
- is 'sega:0 + 0x1878', so end is actually pointing
- to sega:1878 */
-
- printf(); /* Yup, you get 6264 which is 0x1878 */
-
-
- So, the root cause of this is the bane of all MS-DOS programmers:
- Commandment 1: You must treat as a special case any accesses to
- data of length >64k.
-
- No, this does not happen with all pointer calculations--only those
- which wrap-around 64k.
-
- (PS: I believe the other responses were in error)
-
- Adam
-
- --
- Adam G.
- adamg@microware.com, or ...!uunet!mcrware!adamg
- The above is not to be construed in any way as the official or unofficial
- statements of Microware, or any Microware employees.
-