
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!
--
-----------------------------------------
-----------------------------------------