M
mathog
This is an odd one. Normally it doesn't matter if a string is allocated
at run time or if it is set up as an array. In this case it does
though. This causes a segfault at run time:
#define MYMAXSTRING 20000000
int main(int argc, char *argv[]){
char bigstring[MYMAXSTRING];
whereas this works with no problems
#define MYMAXSTRING 20000000
int main(int argc, char *argv[]){
char *bigstring=NULL;
bigstring=malloc(MYMAXSTRING);
if(!bigstring){
(void) fprintf(stderr,"Could not allocate initial memory\n");
exit(EXIT_FAILURE);
}
When run in gdb it shows the segfault on the main() line. There is
plenty of free memory on the system.
It was compiled like:
gcc -Wall -ansi -g -pedantic -o prog prog.c
with gcc 4.2.3 (yes, that is pretty old).
Why the difference?
Thanks,
David Mathog
at run time or if it is set up as an array. In this case it does
though. This causes a segfault at run time:
#define MYMAXSTRING 20000000
int main(int argc, char *argv[]){
char bigstring[MYMAXSTRING];
whereas this works with no problems
#define MYMAXSTRING 20000000
int main(int argc, char *argv[]){
char *bigstring=NULL;
bigstring=malloc(MYMAXSTRING);
if(!bigstring){
(void) fprintf(stderr,"Could not allocate initial memory\n");
exit(EXIT_FAILURE);
}
When run in gdb it shows the segfault on the main() line. There is
plenty of free memory on the system.
It was compiled like:
gcc -Wall -ansi -g -pedantic -o prog prog.c
with gcc 4.2.3 (yes, that is pretty old).
Why the difference?
Thanks,
David Mathog