• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: error: array bound is not an integer constant
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: error: array bound is not an integer constant


  • Subject: Re: error: array bound is not an integer constant
  • From: Daryle Walker <email@hidden>
  • Date: Wed, 3 Aug 2005 05:35:00 -0400

[This is what I get for not reading the e-mail for a few weeks....]

On Jul 15, 2005, at 11:13 AM, Justin C. Walker wrote:

On Jul 15, 2005, at 10:50 , Markian Hlynka wrote:

On Jul 15, 2005, at 9:28, Justin C. Walker wrote:

On Jul 15, 2005, at 01:29 , Markian Hlynka wrote:

I've written some sample code below. This worked fine before, but I just upgraded to xcode 2.1 and tiger 10.4.2. Now it's broken. I need to be able to do this or else way lots of stuff breaks. Can someone help me find a solution, or understand what's going on? The problem seems to be in the typedef line. I don't actually need to do it for an int, but the error is the same. What's going on?

It would help us if you could tell us:
- what the real error is ("it breaks near the typedef line" isn't helpful)
- what version(s) of gcc you have used


I'm sorry. The error was in the subject line. I should have restated it:
error: array bound is not an integer constant

And I could have paid closer attention as well.

And it breaks at the typedef line with gcc 4.0:

Sounds like a bug, unless something in a standard somewhere changed while we weren't watching. Report it to Apple (unless someone here knows for sure).

Before declaring this a bug (assuming you haven't already fixed this), let's look at the code:


#include <iostream>

using namespace std;

#define part1 100
#define part2 50
#define combined (part1 * part2)

#define BOARD_SIZE 7
#define FBS (float)(BOARD_SIZE)
#define MOVESFACTOR ((float)( ((int)( ( (FBS/2.0) * FBS) +0.5)) +12.0))
#define MOVESFACTOR2 ((int)( (MOVESFACTOR/2.0)+0.5) )
#define MAX_LEGAL_MOVES (MOVESFACTOR2 * MOVESFACTOR2)


typedef int Movelist[MAX_LEGAL_MOVES];

int main()
{
   cout<<"done: "<<combined<<endl;
}

I think we need to look at C & C++'s rules on integer-based constants, particularly compile-time ones. The key is that C++ compile-time constants MUST be able to be determined at compile- time. Over the decades, integer types only have a few, and sane, formats throughout computer architectures. Compilers only need discrete symbol processing, so integer expressions are easy to evaluate at compile time. However, there have been so many wacky floating point types, especially ones with precise features that make them hard to simulate, that C++ doesn't support compile-time floating- point constants. (The wackiness would ban cross-compilers.) I think your real error is inexplicably using float-point for your intermediate calculations, disqualifying your code for compile-time computation!


* Natural-number division in C++ takes the "floor" of the result
* You want the "ceiling" of the result

Instead of: (int)(FBS / 2.0 + 0.5)
Try: (BOARD_SIZE / 2 + BOARD_SIZE % 2)

It'll work for both odd and even (positive) integers! If you weren't using 2 as your divisor (e.g. 3), you'll need

    (BOARD_SIZE / N + (0 != BOARD_SIZE % N))

instead because you need a 0 or 1 for the extra addend, not {0, 1, ... N-1}.

Try something like:
    #define CEIL_DIV(n, d)  ((n) / (d) + (0 != (n) % (d)))
    #define SQU(x)          ((x) * (x))

    #define BOARD_SIZE       7
    #define MOVESFACTOR      (CEIL_DIV(SQU(BOARD_SIZE), 2) + 12)
    #define MOVESFACTOR2     (CEIL_DIV(MOVESFACTOR, 2))
    #define MAX_LEGAL_MOVES  (SQU(MOVESFACTOR2))

Macros still have the multiple-evaluation problem, of course.

For the future, 'gcc_select' will tell you exactly what compilers it
knows about:

$ gcc_select -l
Available compiler versions:
3.3             3.3-fast        4.0
$

and these tags are what you use with 'gcc_select'.

-- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT mac DOT com

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: How to set up Subversion+SSH+Xcode
  • Next by Date: Re: decyphering ld's cryptic error messages
  • Previous by thread: How to set up Subversion+SSH+Xcode
  • Next by thread: Debugging into Templates
  • Index(es):
    • Date
    • Thread