Solved

Declaration of array

Posted on 1998-08-21
2
265 Views
Last Modified: 2010-04-15
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
Comment
Question by:peiyoke
2 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1252243
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
 
LVL 11

Expert Comment

by:alexo
ID: 1252244
For DOS or Windows 3.x try:
    huge float rn[HEIGHT][WIDTH], ...
    ^^^^^
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

708 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

13 Experts available now in Live!

Get 1:1 Help Now