Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

Declaration of array

I have been declaring my array in the fashion shown in the code segment below and the max size for my PC is 64x64. What's wrong with my codes? How can I increase the sizes of the arrays to 512x512? Do I have to deal with setting the buffer size or virtual memory?

My declaration of array is as follow:

#define HEIGHT 32
#define WIDTH 32

short  x,y,ro[HEIGHT][WIDTH],go[HEIGHT][WIDTH],bo[HEIGHT][WIDTH];
short  rq[HEIGHT][WIDTH],gq[HEIGHT][WIDTH],bq[HEIGHT][WIDTH];
float  rn[HEIGHT][WIDTH],gn[HEIGHT][WIDTH],bn[HEIGHT][WIDTH];
float  rq2[HEIGHT][WIDTH],gq2[HEIGHT][WIDTH],bq2[HEIGHT][WIDTH];


The way I use the array :

  for (x=0;x<HEIGHT;x++)
     {
       for (y=0;y<WIDTH;y++)
          {
              Infile>>ro[x][y]>>go[x][y]>>bo[x][y];

              rn[x][y]=ro[x][y]/255.0;
              gn[x][y]=go[x][y]/255.0;
              bn[x][y]=bo[x][y]/255.0;

              rq[x][y]=ro[x][y]*15/255;
              gq[x][y]=go[x][y]*15/255;
              bq[x][y]=bo[x][y]*15/255;

              rq2[x][y]=rq[x][y]/15.0;
              gq2[x][y]=gq[x][y]/15.0;
            bq2[x][y]=bq[x][y]/15.0;

       }
  }

Can I have an example of more efficient implementation of arrays ?
0
peiyoke
Asked:
peiyoke
1 Solution
 
Answers2000Commented:
Depends on the compiler.

Assuming DOS or Win3.1 (you mention code segment which implies you are compiling small or medium model on one of these)
64*64*2 (array of shorts) = 8192 bytes
64*64*4 (array of floats) = 16384 bytes

A segment in these environments is 64K.

The easiest solution is to change to LARGE model (in your compiler options).  In DOS/Win3.1 any single variable can be up to 64K in these models.

so
512*512*2 (shorts) = 524288 bytes
512*512*4 (floats) = 1048576 bytes.

Unfortunately this still doesn't fit in one segment.

What you have to is
1. Allocate the memory (GlobalAlloc then GLobalLock in Windows, or compiler specific huge-malloc function in DOS)
2. Cast the pointer to the of the array into a huge pointer
3. Calc position in the array, something like
long offset = (long)( column * sizeof(short) ) + (long)( row * 512 * sizeof(short) )
item_address = start_of_memory + offset
(substitue sizeof(float) for the float arrays)).
4. Access memorys in the array by doing
*item_address = value
or
value = *item_address ;
5. When done free the memory (GlobalUnlock then GlobalFree in Windows, or free in DOS)

For DOS you will need also need a DPMI (DOS Protected Mode Interface) capable compiler or a compiler than knows about DOS extenders.

BTW if you have read in an old Petzold (Win 3.1 version) that Large version is bad news, this only applies to MS C versions 5.1 and 6 - it is fixed in MS C 7.0, MS C 8 and all versions of Visual C and Borland C.

If you switch to NT, Win95 or UNIX you will not have these problems and can declare arrays any size.


0
 
alexoCommented:
For DOS or Windows 3.x try:
    huge float rn[HEIGHT][WIDTH], ...
    ^^^^^
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now