From mboxrd@z Thu Jan 1 00:00:00 1970 From: Greg Rigole To: "'gcc@gcc.gnu.org'" Subject: RE: stat problem ? Date: Fri, 31 Mar 2000 12:49:00 -0000 Message-id: <59C8A3391243D311B5390000E865F4C501CF3B0F@rssesnexb.rogers.com> X-SW-Source: 2000-03/msg00858.html The function stat is defined in the manual (in my case, AIX 4.2.1) as follows: int stat (Path, Buffer) const char *Path; struct stat *Buffer; The stat function takes a pointer to an ALREADY EXISTING BUFFER. It does not allocate the buffer for you. In program #1, you passed an UNITIALIZED POINTER (probably garbage) instead of one pointing to a buffer large enough for a struct stat. The stat function blindly attempts to write using the pointer, which probably points at inaccessible memory. Hence the exception. In program two, you pass a pointer to a buffer assigned to its own valid memory, large enough to hold a struct stat. The stat function now correctly puts the info for the file into that buffer. A third alternative is: main(void) { DIR *dp; struct dirent *ep; struct stat *st; int rc; dp = opendir ("./"); if (dp != NULL) { st=malloc( sizeof( struct stat ) ); if (st == NULL) { puts ("Out of memory."); exit(-1); } while (ep = readdir (dp)) { rc=stat (ep->d_name,st); if (S_ISDIR(st->st_mode)!=0) puts (ep->d_name); else { printf ("Not a directory : "); puts(ep->d_name); printf("\n"); } } free( st ); (void) closedir (dp); } else puts ("Couldn't open the directory."); return; } } In this case, there is extra work involved for a situation where only one buffer is needed for the entire program lifetime, an ideal situation for a static (or in this case automatic) buffer. ----------------------------------------------------------------- PS: Is this group the place for this kind of question? I don't know, I'm just asking. ----------------------------------------------------------------- Greg -----Original Message----- From: Borsos Zoltán [ mailto:zoltan.borsos@sysdata.siemens.hu ] Sent: Thursday, March 30, 2000 1:31 AM To: 'gcc@gcc.gnu.org' Subject: stat problem ? Hi everybody ! I'm new in this list and in C too. I have written a small program that in one version dosen't work but in the other does it. I'd like to ask the experts explain me the difference between the programs. I can compile both free from errors but when I let it run this version give me the following message : [main] D:\KOZOS\progik\c\exe\dirsiz_1.exe 1000 (0) handle_exceptions: Exception: STATUS_ACCESS_VIOLATION [main] dirsiz_1 1000 (0) handle_exceptions: Dumping stack trace to dirsiz_1.exe. core // dirsiz_1.c #include #include main(void) { DIR *dp; struct dirent *ep; struct stat *st; int rc; dp = opendir ("./"); if (dp != NULL) { while (ep = readdir (dp)) { rc=stat (ep->d_name,st); if (S_ISDIR(st->st_mode)!=0) puts (ep->d_name); else { printf ("Not a directory : "); puts(ep->d_name); printf("\n"); } } (void) closedir (dp); } else puts ("Couldn't open the directory."); return; } ------------------------------------------------- This version works fine : // dirsiz_2.c #include #include main(void) { DIR *dp; struct dirent *ep; struct stat st; int rc; dp = opendir ("./"); if (dp != NULL) { while (ep = readdir (dp)) { rc=stat (ep->d_name,&st); if (S_ISDIR(st.st_mode)!=0) puts (ep->d_name); else { printf ("Not a directory : "); puts(ep->d_name); printf("\n"); } } (void) closedir (dp); } else puts ("Couldn't open the directory."); return; } Thanks for the answers ! The answers please to my own address too. Szambi