array problem PLEASE HELP 
Author Message
 array problem PLEASE HELP

I've been doing a small graphics program, but ran into this problem very early
on.  Please someone EMAIL me and let me know what I've done wrong... I can't
see a problem:

#define kArraySize (512*512)

unsigned char gBuffer[kArraySize];

^^ Error here, Must declare at least one array element.

If I declare kArraySize to 512 (or multiply out the number), it's fine.  No
problem.  But shouldn't the preprocessor work the number out before it gives
it to the array declaration?  In this case, it's a simple way around, but
in reality, the full version is going to declare the two dimensions
(say to 320*200) and I want them to be constants I declare in a header file,
not have to search my code for all the array declarations.  So, please,
tell me what I did wrong.  Do I have to set up some environments I'm not
familiar with?  (I'm using BC++4.0 for windows, I've always done my work from
Dos with the command line compiler before this... Win95 makes this a bit of
a pain, so I figured I'd use the IDE this time around).  

Anyways, any help will be much appreciated... Email would be preferred, as
this area is pretty busy, and I'll likely miss an article to myself...

--Flatline--



Thu, 16 Apr 1998 02:00:00 GMT  
 array problem PLEASE HELP


Quote:
>I've been doing a small graphics program, but ran into this problem very early
>on.  Please someone EMAIL me and let me know what I've done wrong... I can't
>see a problem:

>#define kArraySize (512*512)

>unsigned char gBuffer[kArraySize];

>^^ Error here, Must declare at least one array element.

I'll assume that your compiler uses 16 bits for ints.

(512*512) is an expression where 2 ints are multiplied together to give an
int result. The resulting value (262144) cannot be represented in a  16 bit
int so this expression results in undefined behaviour. In your case the
compiler could be reducing it modulo 32768 or 65536 giving a result of 0
(but any result or indeed no result is perfectly valid behaviour for the
compiler). A simple way to correct this is:

#define kArraySize (512L*512)

which forces the multiplication to use long type.

Quote:
>If I declare kArraySize to 512 (or multiply out the number), it's fine.

512 is representable as an int.

If you write 262144 explicitly the compiler will give it a type which
can represent that value (the first of int, long, unsigned long
which is adequate). In your case 262144 would have type long so there is
no problem.

Quote:
> No
>problem.  But shouldn't the preprocessor work the number out before it gives
>it to the array declaration?

The preprocessor does not evaluate expressions except as part of directives
such as #if. What it does do is perform textual substitutions (or more
correctly token substitutions) on the source code. So your code is equivalent
to:

unsigned char gBuffer[512*512];

This is important. For example the code:

#define ELEVEN 4+7

...

a = 2 * ELEVEN;

is equivalent to

a = 2 * 4+7;

which results in 15 and not 22 as you might expect at first glance.

...

Quote:
>Anyways, any help will be much appreciated... Email would be preferred, as
>this area is pretty busy, and I'll likely miss an article to myself...

All you need to do is look for articles in this thread. If your newsreader
doesn't support threads you should find one that does, urgently!

--
-----------------------------------------


-----------------------------------------



Fri, 17 Apr 1998 03:00:00 GMT  
 array problem PLEASE HELP

Quote:

>I've been doing a small graphics program, but ran into this problem very early
>on.  Please someone EMAIL me and let me know what I've done wrong... I can't
>see a problem:

>#define kArraySize (512*512)

>unsigned char gBuffer[kArraySize];

>^^ Error here, Must declare at least one array element.

>If I declare kArraySize to 512 (or multiply out the number), it's fine.

Well, let's see.  The compiler uses ints in calculations, and 512^2 has the
lower 16 bits all zero, so...
--
#include        <standard.disclaimer>
 _
Kevin D Quitt  USA 91351-4454           96.37% of all statistics are made up


Sat, 18 Apr 1998 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. NEED HELP WITH PRITING AN ARRAY, PLEASE PLEASE HELP

2. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

3. problems with array allocation and destruction::: HELP PLEASE

4. Arrays with no pointers - problem (please help!)

5. Problem with array's PLEASE HELP

6. Problems with sorting arrays-Please Help

7. Problems with array elements from user input, please help

8. Urgent please help LPSTR Array function passing problem

9. help please: problems with arrays

10. HELP Please Please (malloc on an array)

11. Please help!!!!Please help!!!!Please help!!!!

12. Please please help!!!!!!!!! (link problems)

 

 
Powered by phpBB® Forum Software