Thread: Illegal Instruction when writting a file

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    Illegal Instruction when writting a file

    Hi all,

    I'm quite a beginner in C programming. After some time working perfectly, it seems to be an error (Illegal Instruction) when writing a file using fprintf. It has taken a lot of time, so I decided to post it. Here is the code:
    Code:
    void calculateMeanTimes(long t_init, long t_rx, long t_proces, long t_proces_max, int frames){
        FILE * time_file;
        float mean_rx;
        float mean_proces;
        float time_init;
        float time_proces_max;
        
        time_init = ((float)t_init)/1000000.0;
        time_proces_max = ((float)t_proces_max)/1000000.0;
        mean_rx = (((float)t_rx)/1000000.0)/frames;
        mean_proces = (((float)t_proces)/1000000.0)/frames;
    
        /* Escritura de los datos en el fichero*/
        time_file=fopen("temporizacion.dat","w");
        
        if (time_file==NULL)
             syslog(LOG_ERR,"Error en prueba de temporización al abrir fichero temporizacion.dat\n");
        
        fprintf(time_file,TIME_HEADER);
        fprintf(time_file,"\n");
        fprintf(time_file,"Tiempo de inicializacion (Procesado de primera trama: %f\n",time_init);
        fprintf(time_file,"Numero de tramas computadas para calcular media y tiempo maximo: %d\n",frames);
        fprintf(time_file,"Tiempo medio de recepcion: %f\n",mean_rx);
        fprintf(time_file,"Tiempo medio de procesado: %f\n",mean_proces);
        fprintf(time_file,"Tiempo medio de total: %f\n",mean_rx + mean_proces);
        fprintf(time_file,"Tiempo maximo de procesado: %f\n",time_proces_max);
        fprintf(time_file,"******************************************");
        fclose(time_file);
    }
    When I comment any "fprintf line" it works (sure it does not do anything, but the programs doesn't throw any error), so it seems to be that the problems arises from the writing commands.

    I'd appreciate any suggestion.

    Thanks in advance!!!
    G.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And you do get the file opened successfully? You probably should return when the file couldn't be opened.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    Yes it is opened

    Yes, I check if the file has been successfully opened in
    Code:
    if (time_file==NULL)
             syslog(LOG_ERR,"Error en prueba de temporización al abrir fichero temporizacion.dat\n");
    It leaves the message in syslog file, and there's nothing there about that erro. I've already print it in stdout and again no error.
    ???

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, you still should return if it's NULL, or it WILL crash!

    Why don't you comment out HALF of the fprintf's. If that works, then uncomment until it fails. If it fails, comment out until it stops failing. Work with commenting out sections, until you get to a point where it is only one fprintf.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    Yes, i thought about it

    Thanks for your help. Yes, i've already comment blocks to find out what forces the error, and the conclusion was that as soon as I uncomment any fprintf line, it cashes,... so it must be the fprintf,... but why? What am I doing wrong?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by guiu View Post
    Thanks for your help. Yes, i've already comment blocks to find out what forces the error, and the conclusion was that as soon as I uncomment any fprintf line, it cashes,... so it must be the fprintf,... but why? What am I doing wrong?
    I can't see anything directly wrong. What happens if you change the fprintf(time_file...) into fprintf(stdout...)?

    That would identify if it's something to do with the time_file or fprintf itself that is wrong.

    What OS are you running this on? Linux/Unix, I guess... If so, you could perhaps run it in gdb and when it crashes, use "backtrace" to find out where it came from...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32
    Code:
    fprintf(time_file,TIME_HEADER);
    I guess this is wrong.. How is defined TIME_HEADER?

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    Hi,

    First, thanks for your suggestions. Let's answer some:
    TIME_HEADER is a "#defined" tag (on the top of the program.c):
    Code:
    #define TIME_HEADER "PRUEBA DE TEMPORIZACION - ALARMAS VANDELLOS II\n"
    So it's a string,... i don't see anyting worg there...

    By the way, misteriously, as it did before, now it runs ok again,... god, programming is killing me... I have just restarted the FTP program I've been using to put the binary file in the machine where the program runs, and now it works,... Grrggfrrgr...

    Can anybody says anything?
    Anyway, thanks a lot for the help. It was my first time here, and, well, I'm sorry but I'll repeat.

    G.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    Ah! You can be thinking that the updated new versions were not being put in the machine where I run the program... No, I commented lines, and it run, then i discommented those lines, it crashed, then I commented and print someting else, and it run printing something,.. I mean, apparently, the ftp did its job,... I don't see the trick...

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you use FTP to transfer files, perhaps you may want to consider the more secure and reliable "scp" which is based on "secure shell" (ssh) protocol.

    And either way, if things are "just strange", you may want to checsum (e.g. md5sum myapp) on both machines to validate that your copied executable is the same as the one you sent.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,712
    First off, make absolutely sure that you're not using NULL.
    Code:
        if (time_file==NULL) {
             syslog(LOG_ERR,"Error en prueba de temporización al abrir fichero temporizacion.dat\n");
             return;
        }
    Second point I would make is that "random" behaviour such as what you describe could be very easily attributable to the memory pool being trashed somewhere else in the program.

    That FILE* you have is often the result of
    Code:
    FILE *fopen ( const char *filename, const char *mode ) {
      FILE *newFile = malloc( sizeof(FILE) );
      // more code
      return newFile;
    }
    So, if there is any code which is messing up the memory pool, then any other code which uses the memory pool could wind up showing a problem (the effect).
    Many library functions also use malloc, so even seemingly robust functions can sometimes get clobbered.


    Unfortunately, finding the root "cause" can be hard work, and may have nothing to do with the observed effect. One things for sure, no amount of staring or tinkering with the code you have will help you figure it out. At best, all you'll do it push the problem around (as evidenced by "oh it works if I do...").

    As you seem to be using Linux, then I would suggest you take a look at valgrind. If nothing else, it should give you some logs which you can post and (hopefully) get some ideas of where to start looking.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM