Thread: Multidimensional array size limit

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    Multidimensional array size limit

    This is crazy. I never saw this. And it took me two days to find it because the compiler wont show any error, the program just crashes when you call this function.
    And the worst thing is that it wont even enter the function.

    Code:
    #include <iostream>
    using namespace std;
    int f1()
    {
        cout << "Entered f1()" << endl;
        const int NumberOfFiles = 4000;
        // this is the line the crashes the program
        char Name[NumberOfFiles][260]= {0};
        return 1;
    }
    
    int main()
    {
        f1();
        return 0;
    }
    Now that I tested it I saw that it wont crash if I set NumberOfFiles to around 3900.
    I cant figure out what is it all about?
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Just run it as you posted it (with 4000 ) and it terminated successfully

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    maybe its memory allocation off the stack, you could be exceeding your maximum - you should use dynamic allocation
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    @std try to increase it to a large number.

    @rogster Thanks. I thought it was the same size as my memory.
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Compiled it with 9000 and it failed ,so rogster was right!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Stack space on most desktop systems is limited to between 1MB and 8MB (by default).

    > char Name[NumberOfFiles][260]
    This is C++, so consider using std::vector<std::string> > Name; instead.

    If you really want to allocate this dynamically as a 2D array of chars, then do this.
    char (*Name)[260] = new char[NumberOfFiles][260];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What you new, you must delete, so just save yourself the trouble and use std::vector<std::string>.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thank you Salem and Elysia.

    I have to use strtok() and strcpy() so I cant use std:string.
    I thought about something like this but it wont compile.
    Code:
        vector<int>vec(5000,'\0');
        char Name[&vec.at(0)][260]= {0};
    Last edited by Ducky; 07-05-2012 at 07:30 AM.
    Using Windows 10 with Code Blocks and MingW.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What are you doing? The vector is supposed to get rid of your char arrays. Eg:

    std::vector<std::string> vec;
    vec.push_back("my string");
    // etc
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Ok I got it, thanks.

    Code:
        string str=" ";
        char buf[99] = "aaa#bbb#ccc";
        char * pch1 = strtok(buf,"#");
        std::vector<std::string> vec;
        str = pch1;
        vec.push_back(str);
    Using Windows 10 with Code Blocks and MingW.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can directly push back pch1. I'd also replace your strtok with std::getline to avoid having to work with C-style strings in the first place.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Awesome, you're an angel!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stopping an Array before reaching size limit.
    By Endothes in forum C Programming
    Replies: 4
    Last Post: 06-24-2012, 11:09 PM
  2. Replies: 8
    Last Post: 01-30-2011, 02:55 PM
  3. Increase size of global multidimensional array
    By 3saul in forum C Programming
    Replies: 4
    Last Post: 04-22-2006, 09:00 PM
  4. Replies: 4
    Last Post: 04-05-2004, 06:49 AM