Like Tree2Likes
  • 2 Post By dwks

crashes during run time

This is a discussion on crashes during run time within the C Programming forums, part of the General Programming Boards category; why is the program crashing when I change 3000 to 4000 on this line char arrFilename[3000][MAX_PATH + 1]; Code: #include ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    25

    crashes during run time

    why is the program crashing when I change 3000 to 4000 on this line
    char arrFilename[3000][MAX_PATH + 1];

    Code:
    
    
    #include <stdio.h>
    #include <string.h>
    #include <dirent.h>
    #include <time.h>
    
    main()
    {
        int i,j; 
    	char path[100] = "c:\\temp";
    	// crashes at 4000????
        char arrFilename[3000][MAX_PATH + 1];
    
    
    	DIR *dir;
        struct dirent *ent;
    
    	dir = opendir(path);
    
    	if (dir != NULL){
    		
    		j = 0;
    		while((ent = readdir (dir)) != NULL) {
    			//printf("j = %d fname: %s\n",j,ent->d_name) ;
    			//copy string to array
    			strcpy(arrFilename[j],ent->d_name);
    			j++;
    		}
    		closedir (dir);
    	} else {
          /* could not open directory */
          perror (path);
          //return EXIT_FAILURE;
        } 
    
        // print array of filenames
        for (i = 0; i < j; ++i) {
            printf("i %d: %s\n",i, arrFilename[i]);
    
        }
        
    
    	
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Since you declare the array as a local variable, it is stored on the stack. Apparently your stack will hold 3000 * (MAX_PATH+1) bytes but not 4000 * (MAX_PATH+1) bytes.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    can you suggest how to fix this?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    There's three ways:
    * increase the stack size (which is a system-dependent thing)
    * declare the array globally
    * create the array dynamically

    Probably the best is to create it dynamically:
    Code:
    #include <stdlib.h>   // for malloc and free
    
    typedef char FILENAME[MAX_PATH + 1];
    
    int main(void) {
        FILENAME *arrFilename = malloc(4000 * sizeof *arrFilename);
        // check that malloc succeeded
    
        // use the array
    
        free(arrFilename);
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    thank you, How do I use the arrFilename, I am still trying to get pointers
    Code:
        // print array of filenames
        for (i = 0; i < j; ++i) {
            printf("i %d: %s\n",i, arrFilename[i]);

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You use it the same way you used it before.
    The only difference is the malloc and free calls.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It's not really a good idea to have a super-large array and hope you'll never encounter a directory with just a few too many files in it. The best kind of solution would be able to scale upwards no matter how many files there were. The typical way of doing this is again with dynamic memory allocation. You use malloc() to grab some memory, and when it's not enough you use realloc() to get even more memory.

    This is more complicated and you might not want to do it right away, but it means that using dynamic memory allocation (even for a fixed size like 4000) is a step in the right direction. In general the heap (which is what malloc uses) is the only good place to put very large pieces of memory, and data structures that could vary greatly in size. Assuming your program has a reasonable amount of memory, you can probably dynamically allocate hundreds of megabytes at least, while the stack size might be fixed at 10MB.

    In case you're curious, the realloc() solution is detailed here: Build an array of strings dynamically
    And also the post below. And the post above. And also the whole thread, really.
    iMalc and rags_to_riches like this.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    FILENAME *arrFilename = (FILENAME *)malloc(10000 * sizeof *arrFilename);
    is this correct?

    i am getting error with this in VS2010, but it worked with command line:
    FILENAME *arrFilename = malloc(10000 * sizeof *arrFilename);

    portal.cpp(51): error C2440: 'initializing' : cannot convert from 'void *' to 'FILENAME (*)'
    1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
    Last edited by *nick; 09-17-2012 at 03:02 PM.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The error means that you're compiling the program as a C++ program instead of a C program. C doesn't require the cast but C++ does.

    Using a filename for the program that ends .c instead of .cpp might force C compilation. There would be a way to change it in the options also.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    thx I have ported this code to win32, but it seems to have a memory leak, it crashes after a minute.
    I created a thread for this:
    memory leak

    did I put it in wrong place? i don't understand why it is crashing?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program crashes at run time but not in debugger
    By mahaju in forum C Programming
    Replies: 5
    Last Post: 03-14-2012, 11:13 AM
  2. Program runs fine, then crashes 5% of the time
    By mat1z in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2011, 03:28 AM
  3. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  4. No compile time errors, exe crashes.
    By RealityFusion in forum C++ Programming
    Replies: 13
    Last Post: 09-06-2005, 12:34 PM
  5. C++: C with Crashes...
    By Sebastiani in forum C++ Programming
    Replies: 12
    Last Post: 10-27-2002, 09:06 AM

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