Link to home
Start Free TrialLog in
Avatar of peiyoke
peiyoke

asked on

Declaring arrays

Hi,

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 ?
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

The way you have it declared now each array is 32X32 elements or a 1024 = 1K of elements  (not 1 k in size, 1 K of elements)  You have 6 arrays of shorts and 6 arrays of floats.
The size of each short array is 1024 elements times 2 bytes per element equals 2K.  The size of each float array is 1024 elements times 4 bytes per element equals 4 K.  All told you then have 36 K in arrays.

If you are running in a DOS program with a single 64K data segment, that means you arrays alone are over half of your aloted storage space.  And that is at 32X32.  512X512 arrays use 256 times the amount of memory.
One option is to move to a 32 bit programming environment where you not be limited to 64 K segments.  However, even there your 512X512 arrays would be considered excessive.  (They are about 9 meg)

If you are programming in DOS, you can use a memory model that has multiple data segments, but even then your individual arrays will be limited to 64K so you will never get close to 512X512.

It might be worth reconsidering your program design.  Do you really have to use these huge arrays?
Avatar of peiyoke

ASKER

netiod,

I am very new in programming. By moving to the 32 environment, what changes should I implement on my codes? I do really need a 512x512 array.
Unless your code makes some "non-portable" assumptions, like assumes that integers have a particular size, or that pointers have a particular size and/or format, there will be no changes you need to make.  You just need to compile the code for a 32 compiler and run it under a 32 bit OS.  Assuming you are runing window 95, 98 or NT, you can use MS VC, Borland C++, or Borland Builder to create 32 bit console programs.  These are 32 bit programs that seem to the user to be DOS programs because they run at the DOS prompt.  They seem to the programmer to be standard C++ programs (they can do I/O to the console--which a regular windows program cannot).  However, unlike a DOS program, they use 32 bit memory addressing.  Any standard C++ program (one that doesn't OS specific/hardware specific code) will compile as a win32 console program with no changes.

Under win32 each array will be 1 meg.  That will work, but that is excessive even for a 32 program.  I suspect you can avoid that.  What are you trying to do?
Avatar of peiyoke

ASKER



I am using Turbo C++ compiler in Windows 95. I guess Turbo C++ that I am currently using is not a 32 bit program. I am trying to read data from colour image, one pixel with 3 colour components of one byte each, one image of size 512X512 pixels. Any idea on how to solve this?

Thanx.
You probably don't have to store all the pixels in memory at the same time.

The other option is to remain a DOS program but use a DOS extender to be able to access higher memory.  That is likely to be a pain however, and seeing as the PC world has moved to true 32 bit programming, doesn't make a lot of sense

A student edition of Borland C++ builder is like $80 and can produce 16 bit DOS, 16 bit windows, 32 bit console and 32 bit windows programs.  You might want to invest in it.  I prefer MS VC, but it is more expensive and only produces 32 console and 32 bit programs.  However its on-line help and debuger are both 1000nds of times better than builder's, for a beginner that is an important consideration...