-2

I got the problem, that I have a given type I must use, that is defined like

const int width=800, height=800;     
typedef int field[width][height]; 

Now when I try to make in instance of this type, I only get an Exception.

To hold it easy, the program just looks like this:

  const int width=800, height=800;     
    typedef int field[width][height];  

    int main(){
        field f; 
    }

When I compile and link it, everythings fine, but when I'm going to run the progrma I get

0 [unknown (0x25D0)] test 4660 cygwin_exception::open_stackdumpfile: Dumping stack trace to test.exe.stackdump
Vagish
  • 2,520
  • 19
  • 32
  • You can use macro constant to define the width and height instead of const int. – danglingpointer Nov 30 '17 at 12:27
  • 3
    Are you using C or C++? Why have you shown an error message describing where a stack trace is accessible to you, but not shown the trace itself? – Useless Nov 30 '17 at 12:27
  • 5
    The size of this array is 2.44MB, that is probably too large for the stack. – mch Nov 30 '17 at 12:27
  • @LethalProgrammer That "can" should be a "must", since using an `int` (`const` or not) for a statically sized array is not allowed; at best, OP will get a variable-length array instead of the statically sized one they presumably wanted. But that's not the specific problem here. – underscore_d Nov 30 '17 at 12:37
  • @underscore_d true, here the problem is more about the size of the array. – danglingpointer Nov 30 '17 at 12:39
  • 1
    @underscore_d You're mistaken. A `const int` can very well be used as a constant expression, as long as it's not a non-static class member. The code compiles fine in all major compilers, and the type of `field` will be `int[800][800]`. – oisyn Nov 30 '17 at 13:57
  • @oisyn No, you're mistaken. See [Can a const variable be used to declare the size of an array in C?](https://stackoverflow.com/questions/18848537/can-a-const-variable-be-used-to-declare-the-size-of-an-array-in-c) It's not interesting what "all major compilers" do, if you've not specified that you've compiled in a Standard mode with warnings on, etc. – underscore_d Nov 30 '17 at 14:04
  • 1
    @underscore_d This is not C, this is C++ (according to tag and title). And in C++, an intialized `const int` is a [Constant expression](http://en.cppreference.com/w/cpp/language/constant_expression). See also [[expr.const](http://eel.is/c++draft/expr.const)] of the C++ standard. – oisyn Nov 30 '17 at 15:00

1 Answers1

0

Most probably, your program runs out of stack space.

Either make the declaration global (move it out of main () into global scope) or find out how you can increase stack space allocated to your program.

tofro
  • 5,640
  • 14
  • 31
  • Or use `new` to allocate the array in heap memory instead of stack memory. – Remy Lebeau Nov 30 '17 at 18:51
  • @RemyLebeau I would not recommend using `new` though. A `std::vector>` seems more appropriate. – oisyn Dec 01 '17 at 10:38
  • @oisyn sure that is the *safe* way to manage it. Doesn't change the fact that `new` is used under the hood. – Remy Lebeau Dec 01 '17 at 10:39
  • @RemyLebeau Regardless, "use new" shouldn't be used as advice for somewhone who is clearly still learning. See also [R.11](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-newdelete) of the C++ Core Guidelines. – oisyn Dec 01 '17 at 13:47