Thread: Segmentation Fault

  1. #1
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187

    Segmentation Fault

    I'm working on a filter, tried GDB but with no luck.

    Code:
    %{
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    FILE *trabtotal;
    int tipo;
    int i;
    %}
    %%
    
    
    ^\\(R|r)(E|e)(L|l)(A|a)(T|t)(O|o)(R|r)(I|i)(O|o)                             {if(tipo==1) {fprintf(trabtotal,"\\documentclass{report}\n\n");}
                                                                                 if (tipo==2) {fprintf(trabtotal,"Esta expressão não existe em HTML");}}
    
    
    %%
    
    
    int yywrap()
    {
       return 1;
    }
    
    
    int main(int argc,char *argv[])
    {   printf("ola");
        for( i=argc; i>0; i--) {
        printf("%s\n", argv[i]) ;}
        if ((strcmp(argv[1],"-l")==0)) {tipo=1;}
        else{
        if ((strcmp(argv[1],"-h")==0)) {tipo=2;}
        }
        yylex();
        printf("Ficheiro convertido com Sucesso.\n\n");
        return 0;
    }
    This is my code.
    Anyone sees anything that may cause this segmentation fault ?

    It doesn't even printf("ola");


  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    With i equals argc; the following is not defined.
    Code:
    argv[i]
    Further note: argv[0] can sometimes be a null pointer for some OSes.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Quote Originally Posted by stahta01 View Post
    With i equals argc; the following is not defined.
    Code:
    argv[i]
    Further note: argv[0] can sometimes be a null pointer for some OSes.

    Tim S.
    Thanks for your reply Tim.
    I believe I understand what you're saying but thing is my program isn't even printing "ola" so it's not even reaching the for.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by DeanWinchester View Post
    Thanks for your reply Tim.
    I believe I understand what you're saying but thing is my program isn't even printing "ola" so it's not even reaching the for.
    Your conclusion is incorrect. Because the string "ola" does not contain a newline, the actual output is buffered for a short while and may not appear straight away.

    argc[i] will be NULL and printing it will cause exactly what you are seeing. Fix the loop bounds.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by stahta01 View Post
    With i equals argc; the following is not defined.
    Code:
    argv[i]
    Not true. The standard specifies that argv[argc] is the NULL pointer. That is not undefined. The result of dereferencing a NULL pointer is undefined.

    Quote Originally Posted by stahta01 View Post
    Further note: argv[0] can sometimes be a null pointer for some OSes.
    That is only true if argc is zero. If argc is greater than zero, then the standard specifies that argv[0][0] shall be the null (zero) character if the program name is not available from the host system. argv[0] cannot be the NULL pointer in that circumstance.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > http://cboard.cprogramming.com/c-pro...yylex-%3B.html
    > New yylex() problem

    This is your third thread regarding virtually the same code snippet. I'm not even really sure how any of your code is supposed to the HTML replacement thing you described, nor am I sure what yylex() is. I also have no idea why every snippet has a bunch of trash characters between the #includes and the actual code.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by memcpy View Post
    nor am I sure what yylex() is. I also have no idea why every snippet has a bunch of trash characters between the #includes and the actual code.
    See post #11:

    http://cboard.cprogramming.com/c-pro...ml#post1099442

    @Dean: use fprintf(stderr, ...) for debug output, it is not buffered, and don't forget the newlines.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault
    By Kalastrian in forum C Programming
    Replies: 8
    Last Post: 01-05-2012, 09:15 PM
  2. Segmentation Fault
    By tlman12 in forum C Programming
    Replies: 1
    Last Post: 10-29-2010, 03:57 PM
  3. Segmentation Fault?
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-10-2008, 07:26 AM
  4. Segmentation Fault
    By jat421 in forum C Programming
    Replies: 6
    Last Post: 04-03-2005, 02:26 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM