Like Tree3Likes
  • 1 Post By Salem
  • 2 Post By Elysia

Multidimensional array size limit

This is a discussion on Multidimensional array size limit within the C++ Programming forums, part of the General Programming Boards category; This is crazy. I never saw this. And it took me two days to find it because the compiler wont ...

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

    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?
    Compiler MSVC++ 2013 with Code::Blocks.

  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,438
    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
    825
    @std try to increase it to a large number.

    @rogster Thanks. I thought it was the same size as my memory.
    Compiler MSVC++ 2013 with Code::Blocks.

  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
    32,855
    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];
    Ducky likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    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
    825
    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 08:30 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    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
    825
    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);
    Compiler MSVC++ 2013 with Code::Blocks.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    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.
    Ducky and laserlight like this.
    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
    825
    Awesome, you're an angel!
    Compiler MSVC++ 2013 with Code::Blocks.

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-25-2012, 12:09 AM
  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, 10:00 PM
  4. Replies: 4
    Last Post: 04-05-2004, 07:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21