Link to home
Start Free TrialLog in
Avatar of _pls_
_pls_

asked on

Allocate more than 64K memory in DOS system

Hi, I'm making a program with TurboC++ (Borland). Default pointer are set to NEAR, then cannot exceed 64K of memory (Near pointer is 2 BYTE long). To allocate more than 64K, I use FAR pointer (4 BYTE long). But it didn't work...

typedef unsigned char BYTE;
BYTE far *pbVar1;

// Like this, it will print : "Not enough of memory"
if((pbVar1=new BYTE far[70000])==NULL)
{ printf("Not enough of memory."); }

I set MODEL HUGE, automatic far data, fast huge pointer...
But never work :(

my computer and configuration detail :
OS : Windows 98b
RAM : 128
Running TurboC++ as "Reboot in DOS mode" or "command prompt window". The problem of memory allocation exist in both mode.

Is someone able to make that program run correctly with TurboC++ ?
-= Thanks =-

/* TestMemory.cpp */
/* Allocate memory to load 10 pictures of 24K. */
#include <stdio.h>

int main()
{
  const unsigned short kiNbPic=10;
  const unsigned short kiSizePic=24000;
  unsigned short iComp1;
  unsigned short iComp2;

  char szPath[]="C:\\output.txt";
  FILE *pOutput;
  char far **pszPic;

  if((pszPic=new far char*[kiNbPic])==NULL)
  { return 1; }

  /************************************************/
  /* I NEVER EXIT THIS 'FOR' BECAUSE THE SYSTEME  */
  /* DON'T ALLOCATE MEMORY AND THEN RETURN 1.     */
  /* IT RETURN 1 WHEN iComp1=3                    */
  /************************************************/
  for(iComp1=0;iComp1<kiNbPic;iComp1++)
  { if((pszPic[iComp1]=new far char[kiSizePic])==NULL)
    { return 1; }

    // Fill memory with a value just for test.
    for(iComp2=0;iComp2<kiSizePic;iComp2++)
    { pszPic[iComp1][iComp2]=(char)iComp1; }
  }

  // Write all the 10 zone of memory (24K) in file.
  // Sometime pointers addresses are same.
  // It will be easy to spot error in the output file.
  if((pOutput=fopen(szPath,"w"))==NULL)
  { printf("Not able to create output file."); }
  else
  { fprintf(pOutput,"NbPic=%d\tSizePic=%d\n",kiNbPic,kiSizePic);
    fprintf(pOutput,"%s\n",szPath);

    for(iComp1=0;iComp1<kiNbPic;iComp1++)
    { fprintf(pOutput,"\nPic no %d\n",iComp1);

      for(iComp2=0;iComp2<kiSizePic;iComp2++)
      { fprintf(pOutput,"%d",pszPic[iComp1][iComp2]); }
    }

    fclose(pOutput);
  }

  // Desallocate memory.
  for(iComp1=0;iComp1<kiNbPic;iComp1++)
  { delete[] pszPic[iComp1]; }
  delete[] pszPic;

  return 0;
}
Avatar of nietod
nietod

Is ther any reason why you are using Turbo C++ and DOS.   That is about a decade out-of date in an industry that undergoes revolutions aver 6 months.  

A 32 bit compiler and a win32 console makes a lot more sense.
Avatar of _pls_

ASKER

Yes, using VC++ should be better.

But, I have a plane to learn C++ well
1- Learn basic C++ (already done)

2- Learn advance C++ DOS (own developed I/O library for modem, video device, keyboard, printer)
(Two lack left : modem and max 64K of memory)

3- Learn advance C++ Windows (API, multi-thread,...)

4- Learn DirectX8.

I look like crazy, maybe, but I want to know wery well both environment DOS (Bios, Interrup, ...) + Windows (API, multi-thread, ...)

The program in the question above, work very well when compile with VC++ 6.0

Thanks
The answer is that you just can't do it.  The 64K limit is real and the memory allocators in 16-bit runtimes can't allocate more than 64K.  While the HUGE pointer seems to be your savior, in fact it is not.  All this does is "emulate" a larger pointer for some supported operations.  It's really slow and really hard to use since not everything you might do in your program will work properly with it.

If you need more than 64K, you need to allocate multiple blocks that are less than or equal to 64K in size and split your data between them manually.  

Yes, it's a real pain which is why nobody mourned the passing of the segmented X86 architecture.
>>but I want to know wery well both environment DOS (Bios, Interrup, ...) +

If it's just to satisfy your curosity, then go for it!!  But this is DEAD information.  BIOS calls, X86 interrupts, segment registers, are all archaic and useless in 32-bit programming.  Once Windows XP is out, DOS will _really_ be gone from Windows.
>> The 64K limit is real and the memory allocators in 16-bit
>> runtimes can't allocate more than 64K
I don't know about Turbo C++, but the 16 bit Microsoft C++'s allowed allocations greater than 64k.

I woudln't waste my time learning abotu technology that is out of date. Learn about those issues wthing the context of the current technology.

 If you want to become a auto-mechanic, you don't start out learning how to saddle train horses,    
ASKER CERTIFIED SOLUTION
Avatar of CSLIU
CSLIU

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
but this is for Turbo C++, not BCB 4.0, does Trubo C++ have a farmalloc?    
And this is for operator new, not malloc?  What happens with new?
>but this is for Turbo C++, not BCB 4.0, does Trubo C++ >have a farmalloc?    

yes.
Turbo C++ is compatible with Borland C++.

>And this is for operator new, not malloc?  What happens >with new?

i don't know, and i don't install the Borland C++ in my computer now because the CD was broken.
BC 4, not BCB 4. There's a couple of years in between them (BCB 4 ~ BC 5.02, BCB 5 ~ BC 5.5). But even in BC 5 you could build 16 bit DOS programs and it's dos related help files are an exact replica (including examples) of the older versions.

You can find this information in Turbo Help (press F1 in the IDE, SHIFT-F1 for the help index), look for 'farmalloc' and 'farcalloc'

Working with DOS far pointers is /not/ easy. A far pointer under DOS consists of two parts, a 16 bit Segment 'pointer' and a 16 bit Offset. Together they form a 20 bit memory address, which can be calculated by:
memory address = 16 * segment + offset

Which has a few nasty consequences:
1- two different far pointers may point at the same memory location
2- pointer calculation done with the offset alone may fail.
3- headaches finding bugs that result from 1 and/or 2

This is not helpfull at all in learning C++

The easiest way is to use a different memory model. From the IDE choose Options / Compiler / Code Generation. The Compact, Large and Huge memory models give you access to >64 K of memory for data.
To allocate >64 K in a /single/ block of memory you'll need to use farmalloc and either the Huge model or good understanding of how segments and far pointers work.
Yeah, I meant BC, not BCB.  BCb is 32 bit only.

>> This is not helpfull at all in learning C++
Its not helpfull at all in using C++!  Its just not helpful at all. :-)

 Using far pointers was always a last resort for exrreme cases....   Its hard to imagine anyone returning to them voluntarily.
Avatar of _pls_

ASKER

Thanks CSLIU.
I will try what you said tonight, and accept tomorrow :)))
If it work, i'll do beautiful dream !!!!!!!!!!!

Thanks nietod and jhance, I understand your point.
But sometime we need to learn the past to understand the present and the futur :P
I assure you this is not one of those cases.

Will you study the architecture of a PDP?  Learn assembly on Z-80.   Write a program on a punch card reader?

If you could ever find the tools you needed to do those things, you would learn nothing that would be applicable to modern programming.
> But, I have a plane to learn C++ well
> 1- Learn basic C++ (already done)

> 2- Learn advance C++ DOS (own developed I/O library for modem, video device, keyboard, printer)
> (Two lack left : modem and max 64K of memory)

> 3- Learn advance C++ Windows (API, multi-thread,...)

> 4- Learn DirectX8.

Learning Windows programming if you want develop Windows applications.

Learning DOS real mode programming or DOS protected mode programming
if you want develop 80x86 embedded systems.

i suggest DJGPP (a 32-bit GNU C++ for DOS) if you develop 80386 DOS protected mode program.
> I assure you this is not one of those cases.

> Will you study the architecture of a PDP?  Learn
> assembly on Z-80.   Write a program on a punch card
> reader?

Some small systems use Z-80 processor.
i.e GameBoy Color.

> I assure you this is not one of those cases.

> Will you study the architecture of a PDP?  Learn
> assembly on Z-80.   Write a program on a punch card
> reader?

Some small systems use Z-80 processor.
i.e GameBoy Color.

> I assure you this is not one of those cases.

> Will you study the architecture of a PDP?  Learn
> assembly on Z-80.   Write a program on a punch card
> reader?

Some small systems use Z-80 processor.
i.e GameBoy Color.

Gameboy uses a z-80???  Are you sure?

They are using 68000 processors to control coffee makers these days, I can't imagine wny someone would use a Z-80 for anything.  It can't save any money.
> Gameboy uses a z-80???  Are you sure?

yes,
"GameBoy Color" uses a 8-MHz Z-80 processor.

> They are using 68000 processors to control coffee makers > these days, I can't imagine wny someone would
> use a Z-80 for anything.  It can't save any money.

GameBoy Color is an old version of GameBoy.
New GameBoy is "GameBoy Advance".
"GameBoy Advance" is using 32-bit ARM processor.

http://www.gameboy.com/system/tech_popup_body.html

SEGA Genesis uses a 68000 CPU processor.

XBox uses a PIII CPU.

http://www.activewin.com/xbox/inside_the_xbox.shtml

so its not being used anymore then.   I really doubt anyone makes Z-80 chips.  You can get 32 bit CPUs for pennies.
> so its not being used anymore then.   I really
> doubtanyone makes Z-80 chips.  You can get 32 bit CPUs
> for pennies.


http://www.pearsonptg.com/preface/0,3772,0130255181:PRE,00.html





Alright, I guess Z-80 is still around.  Hard to believe though.  
>>I really doubt anyone makes Z-80 chips.  

If you were to think that, you'd be wrong!  Z-80s are still made and still available and better and more popular than ever.  These are really great chips for low-cost embedded microcontroller applications.

In fact, the name of the company that makes them is, ZILOG...  It's the remnant of the old ZILOG that was salvaged from the EXXON takeover debacle...
Avatar of _pls_

ASKER

******************************************************
This code is not able to allocate more than 64K...
It's stop at iComp1=2 (not 3 as I said in previous comment)
I'm tired, I will not be able to finish my project :(
Thanks everybody !
******************************************************


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

int main()
{
  const unsigned short kiNbPic=10;
  const unsigned short kiSizePic=24000;
  unsigned short iComp1;
  unsigned short iComp2;

  FILE *pOutput;
  char far **pszPic;
  char szPath[]="C:\\output.txt";

  pszPic=(char far**)farcalloc(kiNbPic,sizeof(char far*));
  if(pszPic==NULL)
  { return 1; }

  // Allocate big zones with the same values.
  // This only a test to verify if 2 pointer are affect to the same address.
  for(iComp1=0;iComp1<kiNbPic;iComp1++)
  { pszPic[iComp1]=(char far*)farcalloc(kiSizePic,sizeof(char));
    if(pszPic[iComp1]==NULL)
    { return 1; }

    for(iComp2=0;iComp2<kiSizePic;iComp2++)
    { pszPic[iComp1][iComp2]=(char)iComp1; }
  }

  // Write entire values of each zones into output file.
  // Good way to spot errors.
  if((pOutput=fopen(szPath,"w"))==NULL)
  { printf("Not able to create output file.");     }
  else
  { fprintf(pOutput,"NbPic=%d\tSizePic=%d\n",kiNbPic,kiSizePic);
    fprintf(pOutput,"%s\n",szPath);

    for(iComp1=0;iComp1<kiNbPic;iComp1++)
    { fprintf(pOutput,"\nPic no %d\n",iComp1);

      for(iComp2=0;iComp2<kiSizePic;iComp2++)
      { fprintf(pOutput,"%d",pszPic[iComp1][iComp2]); }
    }
  }

  for(iComp1=0;iComp1<kiNbPic;iComp1++)
  { farfree(pszPic[iComp1]); }
  farfree(pszPic);

  return 0;
}
Avatar of _pls_

ASKER

Perfect !
As the help said : use farcalloc or farmalloc to allocate more than 64K.

But I don't understand why it doesn't work :((
I will continue another part of my project and come back to this question later :P

Thanks a lot :)
PLS Pierre-Luc Savard
many think pls=please
hehehe
By all means, accept an answer to your question that DOES NOT WORK!!  That will ensure that you get plenty more help in the future.....
Why did you accept a comment with a poor grade.  If the answer wasn't completely helpfull, you should have asked the expert for more help.   You never gave the expert a chance to help you (as far as anyone could tell, farrmalloc() was enough) and then gave him/her a bad grade in return.
Avatar of _pls_

ASKER

Yes maybe I closed the question too soon...
Sorry CSLIU :(

Poor grade is because I want someone who will take this code above and change something to make that stuff work.

The TurboC++ said farcalloc work, but it didn't.
I don't understand why not, but it didn't
This is only a part of the answer :(

Someone fill OK to try to make that program to run correctly ?

I can give 1000 pts as grade Excellent for who will do it !
Would you like I reopen same question ???

waiting your answer
or email me mr_savard@hotmail.com
Avatar of _pls_

ASKER

Yes maybe I closed the question too soon...
Sorry CSLIU :(

Poor grade is because I want someone who will take this code above and change something to make that stuff work.

The TurboC++ said farcalloc work, but it didn't.
I don't understand why not, but it didn't
This is only a part of the answer :(

Someone fill OK to try to make that program to run correctly ?

I can give 1000 pts as grade Excellent for who will do it !
Would you like I reopen same question ???

waiting your answer
or email me mr_savard@hotmail.com
> Perfect !
> As the help said : use farcalloc or farmalloc to
> allocate more than 64K.

> But I don't understand why it doesn't work :((
> I will continue another part of my project and come back > to this question later :P

It work on my computer now.
I found the Borland C++ 4.5 CD.

Did you run the program in Turbo C++ IDE ?

Avatar of _pls_

ASKER

What is the meaning of IDE ???????
Cool :)))
1,press ALT-X exit to DOS after you make the .exe file.
2,run the .exe file.
 
> ******************************************************
> This code is not able to allocate more than 64K...
> It's stop at iComp1=2 (not 3 as I said in previous
> comment)
 
becasue you run the program in TC++ IDE.
 
> I'm tired, I will not be able to finish my project :(
> Thanks everybody !
> ******************************************************
 

> Someone fill OK to try to make that program to run
> correctly ?
 
yes, i did.
i mailed the exectuable file to you.
 
> What is the meaning of IDE ???????

IDE is Integrated Development Environment.