-
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]);
}
}
-
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.
-
can you suggest how to fix this?
-
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;
}
-
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]);
-
You use it the same way you used it before. :)
The only difference is the malloc and free calls.
-
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: http://cboard.cprogramming.com/c-pro...tml#post630519
And also the post below. And the post above. And also the whole thread, really.
-
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
-
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.
-
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:
http://cboard.cprogramming.com/windo...mory-leak.html
did I put it in wrong place? i don't understand why it is crashing?