Thread: declaring large array

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    3

    declaring large array

    Hi I'm fairly new to programming so excuse me if the problem is unclear or if I haven't provided enough info.

    I am running windows vista, I have 2gb memory. I'm compiling in dev c++, which uses g++ compiler.

    Code:
    #define MAX_DSIZE 6000
    
    struct entry
    {
       // length of entry
       int points;
    
       // float to hold wavelength, flux and error
       float wl[MAX_DSIZE];          
       float flux[MAX_DSIZE];  
       float error[MAX_DSIZE];             
    };
    
    int main ()
    {
       int num_files=450;
       struct entry tdata[num_files];
    }

    It compiles fine but when I run it I get a windows error
    "new2.exe has stopped working..."

    It works for num_files <= 27 so I guess it is some kind of memory problem.

    Anyone know how I can fix this?

    Thanks

    Jase
    "

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to use dynamic memory, ie new and delete.
    And in C++, we use const instead of define and we skip the "struct" keyword.
    Unless you actually posted in the wrong section?
    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.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're trying to declare about 31MB on the stack and the default stack size is 1MB, which is more than enough for basically everything that is written properly.

    You can't declare an array based on the value of a variable in C++. It either has to be const or this is actually C code.
    Judging by the extra struct keyword, this is C code.
    In C++ we'd allocate on the heap like this:
    Code:
    entry *tdata = new entry[num_files];
    and later clean it up with this:
    Code:
    delete[] tdata;
    Last edited by iMalc; 06-01-2008 at 06:09 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    3
    I'm writing c really but I use c++ compiler.

    so I have 31mb of data in ~500 files on the hard drive and they need to be accessed 100s of times.

    so is allocating on the heap is the best way? (I don't really understand heap and stack fully but if it is best way I will read up)

    Thanks for help,

    Jason

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yes allocating these on the heap is the best way.
    The stack is really only for variables that have a short lifetime (the life of the function) and are reasonably small.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In this case, it's better to use malloc and free, then. The C way.
    The "struct" keyword is necessary in C, and so is the define, so it's best to keep it that way.
    Suffice to say about stack vs heap:

    Stack:
    - Limited to about ~1 MB.
    - Cleans up automatically after function ends.
    - Faster to allocate on.

    Heap:
    - Limited to a hard-coded limit of 2/3 GB depending on OS (that which don't fit in memory goes into the page file).
    - Does not clean automatically. Data will be there as long as you need it.
    - Need to be freed or you will get a memory leak.
    - Slower to allocate on.
    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.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you have a huge local variable in main, it almost sounds like it should be a static/global variable.

    Also, can I make another suggestion:
    Instead of three arrays in a struct, you probably should have a second struct that holds the three values, and make that into an array. Whether this is good or not depends quite a bit on how you use the values, but I'm sort of guessing that you will access wl, flux and error for each index, rather than all wl then all flux then all error. If my guess is right, then a struct within the struct would be the right thing to do, because the access of the second and third value will be "almost free", as opposed to reading the data from three different places in memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    3
    Thanks for the advice and suggestions.

    I got it working using malloc like so:

    Code:
    #define MAX_DSIZE 6000
    
    struct entry
    {
       // length of entry
       int points;
    
       // float to hold wavelength, flux and error
       float wl[MAX_DSIZE];          
       float flux[MAX_DSIZE];  
       float error[MAX_DSIZE];          
       
    };
    
    int main ( int argc, char *argv[] )
    {   
       int i;
       struct entry *tdata[439];
       for (i=0 ; i<439 ; i++)
       {
          if ( (tdata[i] = (entry *) malloc(sizeof(entry))) == NULL )
          {
             fprintf(stderr, "\nOut of memory");
             exit(1);
          }
       }
       
       return 0;
    }
    Last edited by jmb52b; 06-02-2008 at 09:12 AM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't forget to free all that memory later!
    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
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    is there a reason you need it to be in all C?

    this seems like it could be a lot cleaner in C++.

    Code:
    class entry
    {
       public:
       // length of entry
       unsigned int points;
    
       // float to hold wavelength, flux and error
       float *wl;          
       float *flux;  
       float *error;          
       entry():
          wl(new float(6000)),
          flux(new float(6000)),
          error(new float(6000))
       {
       }
       ~entry()
       {
          delete[] wl;
          delete[] flux;
          delete[] error;
       }
    };
    
    int main ( int argc, char *argv[] )
    {
       
       try
       {
          entry *tdata = new entry[439];
       }
       catch(...)
       {
             fprintf(stderr, "\nOut of memory");
             exit(1);         
       }
       
       return 0;
    }

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Everything is always better and cleaner in C++. That's because C++ was designed to be an extension of C.
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    A note from GotW #66: Constructor Failures concerning m37h0d's example:
    Moral #3: Always perform unmanaged resource acquisition in the constructor body, never in initializer lists. In other words, either use "resource acquisition is initialization" (thereby avoiding unmanaged resources entirely) or else perform the resource acquisition in the constructor body.

    Moral #4: Always clean up unmanaged resource acquisition in local try-block handlers within the constructor or destructor body, never in constructor or destructor function-try-block handlers.
    Incidentally, the exception to catch is std::bad_alloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    so any allocations using new should be inside the constructor block, wrapped in the try statement?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    so any allocations using new should be inside the constructor block, wrapped in the try statement?
    Yes, if there is more than one of them, but you might as well be consistent.
    Last edited by laserlight; 06-02-2008 at 11:22 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    i see. i got quipped for not using initialization lists before, and was pointed to a ref that said (more or less) to always use initialization lists. obviously if you needed to use exception handling, you'd need to make the declarations in the constructor block.

    i don't mean to hijack the thread, but can you tell me what is meant by "unmanaged resource"? i am assuming this is anything that is not automatically allocated or deallocated by the default constructor/destructors? is this correct?

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack Overflow when declaring array
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2009, 05:00 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. help with declaring array in which the user specifies size
    By ibanezrgking in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 10:05 AM