Thread: Bad address when using stat() and perror()

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    2

    Bad address when using stat() and perror()

    Hello!

    Im a fresh C programmer, and have a issue with this code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <errno.h>
    
    int returnValue;
    uid_t uid;
    
    main( int argc, char* argv[] )
    {	
    	struct stat *filstat;
    	
    	returnValue = fstat( open(argv[1], O_RDWR ), filstat );
    	
    	if(returnValue != 0) {
    		perror("Error");
    		exit(1);
    	}
    	
    	uid = filstat->st_uid;
    	
    	
    	exit(0);
    	
    }
    Like this, the code works. BUT, if I move line 9, uid_t uid; to say line 14, just before returnValue =... it doesnt work! The error I recieve is:
    Error: Bad address

    We and two buddies have been fickling with this issue for two hours now, not understanding why it wont work.

    Do you have any ideas?

    Also, this code works on OSX Tiger 10.4.9 (gcc 4.0.1), but not on Fedora 6 (gcc 4.1.2), which gets a Segmentation Fault.
    Last edited by tore-; 05-23-2007 at 10:15 AM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You can't just declare a pointer and use it, it has to point somewhere. Instead of declaring the stat structure as a pointer, declare it as a real structure. Then pass its address to fstat() using the & operator.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > struct stat *filstat;
    > returnValue = fstat( open(argv[1], O_RDWR ), filstat );
    It's not enough just to declare a variable of the right type just to make the compiler shut up, you have to understand what is really going on. fstat() fills in information into a buffer you supply.

    Code:
    struct stat filstat;
    returnValue = fstat( open(argv[1], O_RDWR ), &filstat );
    uid = filstat.st_uid;
    You're still passing a pointer to fstat(), but it really is pointing to some of your memory.


    You really should separate the open() call, assign the result to another variable, and check it before calling fstat()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    2
    We are WELL aware of bad code structure, but we have tested every option there is!

    But this, we haven&#180;t tested.

    Again, THANKS alot. This cleared out much for us.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  2. that damn stat()
    By @licomb in forum Linux Programming
    Replies: 2
    Last Post: 08-22-2001, 04:24 PM