G
Greg
Please take a look at
#include <stdio.h>
int main() {
char *cstring;
FILE *fp;
fp = fopen( "proc.rc", "r" );
fscanf(fp,"%s", cstring);
printf("%s", cstring);
return 0;
}
The above code works on Utopia (Sun OS 5.7) and on Saturn (Linux 2.6.7)
but it seg faults on artoo (FreeBSD 4.10) and on arcadia (NetBSD 1.6.1)
So, I changed *cstring to an array of 100 chars like so...
#include <stdio.h>
int main() {
char cstring[100];
FILE *fp;
fp = fopen( "proc.rc", "r" );
fscanf(fp,"%s", cstring);
printf("%s", cstring);
return 0;
}
....and this code works flawlessly on all platforms. I'm having trouble
explaining the cause of this to a friend. He believes Linux, Sun OS and
even NetBSD 2 magically allocate memory on the fly (this is speculation
so far). I don't believe that's standard in the C language to depend on
such facilities. My understanding is that *cstring is uninitialized and
shouldn't be assigned segments of data unless it points to an allocated
space of memory (such as an array or segments allocated by malloc)
Please clear up ANY misconceptions.
Thank you,
-Greg
#include <stdio.h>
int main() {
char *cstring;
FILE *fp;
fp = fopen( "proc.rc", "r" );
fscanf(fp,"%s", cstring);
printf("%s", cstring);
return 0;
}
The above code works on Utopia (Sun OS 5.7) and on Saturn (Linux 2.6.7)
but it seg faults on artoo (FreeBSD 4.10) and on arcadia (NetBSD 1.6.1)
So, I changed *cstring to an array of 100 chars like so...
#include <stdio.h>
int main() {
char cstring[100];
FILE *fp;
fp = fopen( "proc.rc", "r" );
fscanf(fp,"%s", cstring);
printf("%s", cstring);
return 0;
}
....and this code works flawlessly on all platforms. I'm having trouble
explaining the cause of this to a friend. He believes Linux, Sun OS and
even NetBSD 2 magically allocate memory on the fly (this is speculation
so far). I don't believe that's standard in the C language to depend on
such facilities. My understanding is that *cstring is uninitialized and
shouldn't be assigned segments of data unless it points to an allocated
space of memory (such as an array or segments allocated by malloc)
Please clear up ANY misconceptions.
Thank you,
-Greg