TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Dan Green
from: David Nugent
date: 1994-12-15 10:06:20
subject: farmalloc

> Is there a C guru out there who can help me? I've been tring
 > to understand far farmalloc, but everything I try crashes my
 > system.

It probably isn't 'farmalloc' that's giving you problems exactly, but code
later on in the example program.

 > #include
 > #include

For compatibility, make these lower case. On a case sensitive filesystem,
you'd fail to open the correct files.

 > main(void){
 > char far *plane1;
 > char far *plane1ptr;
 > long i;
 >    if((plane1=(char *)farmalloc(80000))==NULL){

Don't cast to "char *", as this could (and usually would) cause
truncation of the 'segment' part of the pointer. If you need  to cast it
(and you shouldn't need to), make it "char far *".

 >          fprintf(stderr,"Couldn't get memory\n");
 >          return(1);
 >       }
 >      else
 >      {   fprintf(stdout,"Memory Allocated");
 >          plane1ptr=plane1;
 >         for(i=0;i<80000;i++){
 >          *plane1ptr='A';
 >          plane1ptr+=sizeof(char);

++plane1ptr will do fine. sizeof(char)==1 is guaranteed in C.

However, there's still a problem here. The problem is that you're
incrementing a far pointer, which will wrap at on offset of 64K (or 65536
bytes). If you want the compiler to generate code which will address _all_
of that memory, then you need  to use a huge pointer, which will do the
correct thing at segment boundaries.

 >           }

 >          fprintf(stdout,"Memory is filled!\n");
 >          plane1ptr=plane1;
 >          for(i=0;i<80000;i++){
 >            fprintf(stdout,"%c",*plane1ptr);
 >            plane1ptr+=sizeof(char);

Likewise.

 >            }
 >        farfree(plane1);
 >          }
 >      return(0);


#include 
#include 

int main()
{
    char far *plane1;
    char huge *plane1ptr;
    long i;

    if((plane1=farmalloc(80000))==NULL)
    {
        fprintf(stderr,"Couldn't get memory\n");
        return(1);
    }
    else
    {
        fprintf(stdout,"Memory Allocated");

        plane1ptr=(char huge *)plane1;
        for (i=0;i<80000;i++)
          *plane1ptr++='A';
        fprintf(stdout,"Memory is filled!\n");

        plane1ptr=(char huge *)plane1;
        for(i=0;i<80000;i++)
            fprintf(stdout,"%c",*plane1ptr++);

        farfree(plane1);
    }
    return(0);
}

---

* Origin: Unique Computing, Melbourne, Australia (3:632/348)
SEEN-BY: 50/99 54/54 620/243 623/630 624/50 632/103 301 348 386 998 633/371
SEEN-BY: 633/379 634/384 635/301 502 503 541 544 636/100 639/100 711/401 409
SEEN-BY: 711/410 430 510 807 808 809 932 934 942 712/623 713/888 714/906
SEEN-BY: 800/1
@PATH: 632/103 348 635/503 50/99 54/54 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.