Problem with const int.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Problem with const int.

Hybrid View

  1. #1
    Join Date
    Sep 2010
    Posts
    15

    Problem with const int.

    Sorry to ask simple question, but, even after googling I couldn't solve my problem. Hope you guys can help me.
    Code:
    const int i=100000;
    const int j=32;
    __int64 A[i][j];
    ...
    It compiles but when I run the .exe file it gives an error. The error is like:
    .exe has encountered a problem and needs to close. We are sorry for the inconvenience. ....... Send error report/Don't send.
    But, when I decrease one zero in i's value (i.e. when const int i=10000 it works fine.
    What is the problem here and how can I solve it?
    Thank you.
    Last edited by ritika_sharma82; February 9th, 2011 at 08:23 AM.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,098

    Re: Problem with const int.

    You're probably running out of stack space.

  3. #3
    Join Date
    Sep 2010
    Posts
    15

    Re: Problem with const int.

    Quote Originally Posted by GCDEF View Post
    You're probably running out of stack space.
    Hi GCDEF,
    How can I solve it.
    Any quick and easy solution will be appreciated.
    I am using MS Visual c++ 6.0

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,722

    Re: Problem with const int.

    Is this a local variable?
    If so, you are trying to allocate 25,600,000 bytes on the stack.
    (100000 * 32 * 8)
    That is highly unlikely to work on any machine! The default on Windows machines is 1MB per thread (I think).

    You will have to allocate from the heap, either with a 'new' or use a std::vector.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  5. #5
    Join Date
    Sep 2010
    Posts
    15

    Re: Problem with const int.

    Quote Originally Posted by JohnW@Wessex View Post
    Is this a local variable?
    If so, you are trying to allocate 25,600,000 bytes on the stack.
    (100000 * 32 * 8)
    That is highly unlikely to work on any machine! The default on Windows machines is 1MB per thread (I think).

    You will have to allocate from the heap, either with a 'new' or use a std::vector.
    Hello JohnW,
    These are the local variables. Could you please give me an example? How to use heap?
    My array is exactly A[100000][3]. So, it comes (100000 * 3 * 8)/(1024*1024) = 2.28MB.

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,722

    Re: Problem with const int.

    Using std::vector

    Code:
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        const int i = 100000;
        const int j = 32;
    
        vector<vector<__int64>> A(i, vector<__int64>(j));
    }
    Though if you are going to have large 2D arrays then it is often better to wrap a 1D vector in a class and let the interface emulate the 2D aspect. In that way there is only one allocation of memory and a lot less heap fragmentation.
    You can write your own fairly simply or Use Boost's multi_array.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center