Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Declaration of array

Posted on 1998-08-21
2
268 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

792 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