Solved

large ARRAY-variable (Stack Overflow)

Posted on 2004-04-05
7
634 Views
Last Modified: 2008-01-09
Im trying to declare a LARGE array, but i get the EStackOverflow-exception :-(
Maybe I could do this in a better way by using GETMEM and pointers, but how??

The code is following:

procedure TForm1.Button1Click(Sender: TObject);
const
  MaxY=600;
  MaxX=1600;

type
  tboardType=Array[0..MaxY] of Array[0..MaxX] of byte;

var
  cb,tb:tboardtype;

begin
  FillChar(cb,SizeOf(cb),0);
  FillChar(tb,SizeOf(tb),0);
end;
0
Comment
Question by:hutelihut
  • 4
  • 2
7 Comments
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 10756873
taken from the delphi help file

"Static array types are denoted by constructions of the form

array[indexType1, ..., indexTypen] of baseType

where each indexType is an ordinal type whose range does not exceed 2GB. "

your array exceeded 2GB
0
 

Accepted Solution

by:
TomGrills earned 130 total points
ID: 10756906
Here's one way.....

procedure TForm1.Button1Click(Sender: TObject);
const
  MaxY=600;
  MaxX=1600;

type
// First, define a pointer to the static type
  PboardType=^TBoardType;
  tboardType=Array[0..MaxY] of Array[0..MaxX] of byte;

var
  cb,tb:Pboardtype;
  testbyte:byte;
begin
  // Now you must get the memory for the arrays;
  getmem(cb,sizeof(TBoardType));
  getmem(tb,sizeof(TBoardType));

  // Always dereference your pointer when in use
  FillChar(cb^,SizeOf(cb^),0);
  FillChar(tb^,SizeOf(tb^),0);

  //  To Access values...
  Testbyte:=cb^[2][44];

  //  When finished, you have to free the pointers
  freemem(cb);
  freemem(tb);
end;
0
 

Expert Comment

by:TomGrills
ID: 10756964
Jase-Coder,

   His array is well within the 2gb limit...  it's only 600*1600 elements or 960000 bytes
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 11

Expert Comment

by:Jase-Coder
ID: 10768122
Tom: I complied a copy and it said it exceeds the 2GB limit
0
 

Author Comment

by:hutelihut
ID: 10801027
hmm... I got a little problem... Delphi comes up with a warning "Unsafe code Getmem". What does that mean??
0
 

Expert Comment

by:TomGrills
ID: 10801093
It just means that it won't be portable to .NET....  Here's the quote:

Unsafe code warnings in Delphi
Starting with Delphi 7, the compiler includes three new warnings that can help you locate code that is non-portable (i.e. it uses a deprecated language feature or type), or unsafe in the .NET Framework. In the .NET Framework, "unsafe" simply means that the code cannot be verified by the static analysis performed by the CLR when the code is loaded. The compiler can warn about the usage of unsafe types, unsafe code, and unsafe casts.

Unsafe types
  PChar, PWideChar, and PAnsiChar  
  Untyped pointers
  Untyped var and out parameters  
  File of <type>
  Real48
  Variant records (records containing overlapping fields)

Unsafe code
  Absolute variables  
  Addr(), Ptr(), Hi(), Lo(), Swap() standard procedures
  BlockRead(), and BlockWrite()
  Fail()
  GetMem(), FreeMem(), ReallocMem()
  inline assembler
  the @ operator

......


0
 

Expert Comment

by:TomGrills
ID: 10801104
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

19 Experts available now in Live!

Get 1:1 Help Now