Solved

Allocate more than 64K memory in DOS system

Posted on 2001-07-05
33
495 Views
Last Modified: 2011-09-20
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;
}
0
Comment
Question by:_pls_
  • 11
  • 9
  • 7
  • +2
33 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 6256377
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.
0
 

Author Comment

by:_pls_
ID: 6256698
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
0
 
LVL 32

Expert Comment

by:jhance
ID: 6256738
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.
0
 
LVL 32

Expert Comment

by:jhance
ID: 6256748
>>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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6256783
>> 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,    
0
 
LVL 1

Accepted Solution

by:
CSLIU earned 200 total points
ID: 6256806

void far *farptr;

farptr = (void*)farmalloc( 800000L );
if( farptr == NULL )
{
  printf("\nNot Enough Memory");
}
else
{
  farfree( farptr );
}

Borland C++ 4.0 Library reference. page 92, page 93.

farmalloc
farfree

for allocating from the far heap, note that

.All available RAM can be allocated.
.Blocks larger than 64K can be allocated.
.Far pointers are used to access the allocated blocks.

0
 
LVL 22

Expert Comment

by:nietod
ID: 6256819
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?
0
 
LVL 1

Expert Comment

by:CSLIU
ID: 6256994
>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.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6257160
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6257176
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.
0
 

Author Comment

by:_pls_
ID: 6257222
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
0
 
LVL 22

Expert Comment

by:nietod
ID: 6257346
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.
0
 
LVL 1

Expert Comment

by:CSLIU
ID: 6258938
> 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.
0
 
LVL 1

Expert Comment

by:CSLIU
ID: 6258951
> 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.

0
 
LVL 1

Expert Comment

by:CSLIU
ID: 6259015
> 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.

0
 
LVL 1

Expert Comment

by:CSLIU
ID: 6259035
> 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.

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:nietod
ID: 6259119
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.
0
 
LVL 1

Expert Comment

by:CSLIU
ID: 6259281
> 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

0
 
LVL 22

Expert Comment

by:nietod
ID: 6259373
so its not being used anymore then.   I really doubt anyone makes Z-80 chips.  You can get 32 bit CPUs for pennies.
0
 
LVL 1

Expert Comment

by:CSLIU
ID: 6259539
> 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





0
 
LVL 22

Expert Comment

by:nietod
ID: 6259582
Alright, I guess Z-80 is still around.  Hard to believe though.  
0
 
LVL 32

Expert Comment

by:jhance
ID: 6259589
>>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...
0
 
LVL 32

Expert Comment

by:jhance
ID: 6259591
0
 

Author Comment

by:_pls_
ID: 6259644
******************************************************
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;
}
0
 

Author Comment

by:_pls_
ID: 6259694
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
0
 
LVL 32

Expert Comment

by:jhance
ID: 6259738
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.....
0
 
LVL 22

Expert Comment

by:nietod
ID: 6259820
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.
0
 

Author Comment

by:_pls_
ID: 6259972
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
0
 

Author Comment

by:_pls_
ID: 6259987
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
0
 
LVL 1

Expert Comment

by:CSLIU
ID: 6260069
> 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 ?

0
 

Author Comment

by:_pls_
ID: 6260178
What is the meaning of IDE ???????
Cool :)))
0
 
LVL 1

Expert Comment

by:CSLIU
ID: 6260194
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.
 
0
 
LVL 1

Expert Comment

by:CSLIU
ID: 6260199
> What is the meaning of IDE ???????

IDE is Integrated Development Environment.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now