Thread: declaring a large array problem !

  1. #1

    declaring a large array problem !

    hi ! i am having a problem with declaring a 3 dimensional array !!
    can any one help me declare

    a[150][25000][150] ;

    in C++. can it be possible ? plz help me thanks !

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >a[150][25000][150] ;
    I seriously doubt you need an array this large. If you do then you have some design issues to hammer out before writing anything.

    My best code is written with the delete key.

  3. #3
    Join Date
    Nov 2002
    You can't need a array that big, that array has twice as many elements as there are people living in the United States.

    Describe what you are trying to do and maybe we can suggest a better solution to your problem.

  4. #4
    well what i am trying to do is that i am sorting a file of 25000 elements with a max of 150 letters per number. now when i read a number then i can store it in a[0][0][1...150] by reference to its length so that i dont have to make any comparison for variable length digits. suppose that i have a number of 50 length then i can store it at a[50[0][1...50] , no when another number of 5 length comes then ican store it in a[50][1][1...50] ... so in this way i will be able to make less comparisons as ill already know that the elemnts at a[50][..][...] are less greater than a[49][..][..] so ill save comparisons. and if all the elemenst are of same size then i have to store them in say a[30][1....25000][30]. this is what i am trying to do !!!

  5. #5
    Registered User
    Join Date
    Jan 2003
    well.. your array declaration should always begin with a "int" or "char" or whatever..

  6. #6
    Registered User
    Join Date
    Jan 2003
    Premature optomization is the root of all evil. Try this.

    using std::cout;
    using std::endl;
    bool cmp(const std::string &a,const std::string &b) {
      if(a.size()<b.size()) return true;
      else if(a.size()>b.size()) return false;
      else return (a<b);
    int main() {
      std::vector<std::string> strings;
      cout << "loading" << endl;
      std::ifstream in("bigfile");
      while(!in.eof()) {
         std::string str;
      cout << "sorting" << endl;
      cout << "done" << endl;
    The load takes about 5 seconds on a slow machine the slowest sort took was 0.7 seconds and I don't understand that one, it takes ~0.4 seconds usually. My test file was about 2 megs 25000 random strings of random length from 1 to 150.

  7. #7
    Registered User
    Join Date
    Sep 2001
    theres an formula somewhere that helps you reference all the elements with just one variable.. that if the numers all fit in hex or something lol.

  8. #8
    Join Date
    Nov 2002
    Originally posted by Array_troubled
    well what i am trying to do ...
    Ok, I get it. What you're trying to do makes sence speed wise, but unfortunatly you have to live within the memory limits of the computer as well.

    One option would be to use some Linked Lists instead of all arrays so that you will not have to allocate memory until it is needed to hold something; of course however, this will take more time to index then an array would.

  9. #9
    grib.... i don't have the include files that you have mentioned.... is this code for VC???? i need this sorting to work in Turbo C++ ... can i do it somehow????? please reply as soon as possible...

  10. #10
    Registered User
    Join Date
    Jun 2002
    Perhaps you can try using dynamic memory allocation using the new operator and pointers.

  11. #11
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    i would like to know what is happening. what elements are you sorting? why are there so many? and is it worth all this fuss?

    but without knowing a single thing, the best advice i could give for large arrays is to sort it in small blocks. sort a block, then either output it to file or screen, and forget about it. there's no way you can keep it all at once. but do tell me what the whole idea is.
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 07:53 AM
  2. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:26 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM