Segmentation fault with array

Discussion in 'C Programming' started by ptq2238@gmail.com, May 19, 2007.

  1. Guest

    Hi, I'm getting confused with arrays and hope someone can shed light
    on my code.
    I wrote this to try and learn about files and arrays as I thought if I
    could grab each element and place them into an array I can manipulate
    the stings from the file with array indexes.
    Perhaps there's a better method but I'm learning.

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    char c;
    int num=0;
    int count=0;
    int i;
    char instring[10];

    FILE * fp;

    if((fp=fopen("testdata.txt","r"))==NULL)
    {
    printf("Error in openning the file.\n");
    exit(2);
    }

    while(!feof(fp))
    {
    num++;
    if ((c = getc(fp))==EOF)
    {
    printf("EOF reached \n");
    break;
    }
    else
    {
    if (c != '\0')
    {
    instring[count] =c;
    printf("count = %d, instring = %c\n",count,instring[count]);
    count++;
    }
    }
    }

    i=0;
    printf("char 3 is %c\n",instring[i+3]);

    return 0;
    }

    The testdata.txt contains
    ab$
    e.f
    1we
    @\/
    "@'
    :]_
    ..

    When I execute the program I get the following result:
    count = 0, instring = a
    count = 1, instring = b
    count = 2, instring = $
    count = 3, instring =

    count = 4, instring = e
    count = 5, instring = .
    count = 6, instring = f
    count = 7, instring =

    count = 8, instring = 1
    count = 9, instring = w
    count = 10, instring = e
    count = 11, instring =

    count = 12, instring = @
    count = 13, instring = \
    count = 14, instring = /
    count = 15, instring =

    count = 16, instring = "
    count = 17, instring = @
    count = 18, instring = '
    count = 19, instring =

    Segmentation Fault (core dumped)

    My question is if the instring has 10 elements then why is it printing
    out to element 19 ?
    If I put a large number, such as char instring[150], then it will work

    Have I misunderstood arrays completely ?

    Pat
    , May 19, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > Hi, I'm getting confused with arrays and hope someone can shed light
    > on my code.
    > I wrote this to try and learn about files and arrays as I thought if I
    > could grab each element and place them into an array I can manipulate
    > the stings from the file with array indexes.
    > Perhaps there's a better method but I'm learning.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main()
    > {
    > char c;
    > int num=0;
    > int count=0;
    > int i;
    > char instring[10];
    >
    > FILE * fp;
    >
    > if((fp=fopen("testdata.txt","r"))==NULL)
    > {
    > printf("Error in openning the file.\n");
    > exit(2);
    > }
    >
    > while(!feof(fp))
    > {
    > num++;
    > if ((c = getc(fp))==EOF)
    > {
    > printf("EOF reached \n");
    > break;
    > }
    > else
    > {
    > if (c != '\0')
    > {
    > instring[count] =c;
    > printf("count = %d, instring = %c\n",count,instring[count]);
    > count++;
    > }
    > }
    > }
    >
    > i=0;
    > printf("char 3 is %c\n",instring[i+3]);
    >
    > return 0;
    > }
    >
    >
    > My question is if the instring has 10 elements then why is it printing
    > out to element 19 ?
    > If I put a large number, such as char instring[150], then it will work
    >
    > Have I misunderstood arrays completely ?
    >

    You have an array of single characters, which you then fill, and keep
    writing to without checking if you have written past the end. It looks
    like you intended to read lines, rather than single characters.

    --
    Ian Collins.
    Ian Collins, May 19, 2007
    #2
    1. Advertising

  3. Michael Guest

    Ian answered your actual question, so let me point out another problem
    in your code:

    > char c;

    ....
    > if ((c = getc(fp))==EOF)


    The function getc returns an integer. I know, that's crazy, but
    that's the way it is. (Not all standard library functions have good
    interfaces.) The comparison to EOF can fail (I forget whether it
    always does or only does on certain implementations) if c is a
    character instead of an int.

    Michael
    Michael, May 19, 2007
    #3
  4. Chris Dollin Guest

    Michael wrote:

    > Ian answered your actual question, so let me point out another problem
    > in your code:
    >
    >> char c;

    > ...
    >> if ((c = getc(fp))==EOF)

    >
    > The function getc returns an integer. I know, that's crazy, but
    > that's the way it is.


    Suppose `getc` returned a `char`. What `char` value would you
    pick to represent EOF and hence disallow from appearing in any
    FILE* input?

    Remember that FILE*s can be reading "binary" files.

    --
    Three-Way Secret Hedgehog
    A rock is not a fact. A rock is a rock.
    Chris Dollin, May 19, 2007
    #4
  5. wrote:

    > Hi, I'm getting confused with arrays and hope someone can shed light
    > on my code.
    > I wrote this to try and learn about files and arrays as I thought if I
    > could grab each element and place them into an array I can manipulate
    > the stings from the file with array indexes.
    > Perhaps there's a better method but I'm learning.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main()
    > {
    > char c;
    > int num=0;
    > int count=0;
    > int i;
    > char instring[10];
    >
    > FILE * fp;
    >
    > if((fp=fopen("testdata.txt","r"))==NULL)
    > {
    > printf("Error in openning the file.\n");
    > exit(2);
    > }
    >
    > while(!feof(fp))
    > {
    > num++;
    > if ((c = getc(fp))==EOF)
    > {
    > printf("EOF reached \n");
    > break;
    > }
    > else
    > {
    > if (c != '\0')
    > {
    > instring[count] =c;
    > printf("count = %d, instring = %c\n",count,instring[count]);
    > count++;
    > }
    > }
    > }
    >
    > i=0;
    > printf("char 3 is %c\n",instring[i+3]);
    >
    > return 0;
    > }
    >
    > The testdata.txt contains
    > ab$
    > e.f
    > 1we
    > @\/
    > "@'
    > :]_
    > .
    >
    > When I execute the program I get the following result:
    > count = 0, instring = a
    > count = 1, instring = b
    > count = 2, instring = $
    > count = 3, instring =
    >
    > count = 4, instring = e
    > count = 5, instring = .
    > count = 6, instring = f
    > count = 7, instring =
    >
    > count = 8, instring = 1
    > count = 9, instring = w
    > count = 10, instring = e
    > count = 11, instring =
    >
    > count = 12, instring = @
    > count = 13, instring = \
    > count = 14, instring = /
    > count = 15, instring =
    >
    > count = 16, instring = "
    > count = 17, instring = @
    > count = 18, instring = '
    > count = 19, instring =
    >
    > Segmentation Fault (core dumped)
    >
    > My question is if the instring has 10 elements then why is it printing
    > out to element 19 ?
    > If I put a large number, such as char instring[150], then it will work
    >
    > Have I misunderstood arrays completely ?


    What you seem to be missing is that in C it is *your* responsibility, as
    a programmer, to ensure that you will not go outside the array bounds.
    The compiler is not required (and sometimes even not able) to check that
    you stay within array bounds.

    In your code, you were happily clobbering over some random memory, until
    the processor determined that something was seriously wrong. This is
    perfectly acceptable behaviour if the programmer forgets his/her duty
    to check array bounds. (By accessing memory outside the array bounds,
    you invoke undefined behaviour. Anything the computer does then is
    fully correct.)

    >
    > Pat


    Bart v Ingen Schenau
    --
    a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
    c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    c.l.c++ FAQ: http://www.parashift.com/c -faq-lite/
    Bart van Ingen Schenau, May 19, 2007
    #5
  6. On 18 May 2007 22:28:50 -0700, wrote:

    >Hi, I'm getting confused with arrays and hope someone can shed light
    >on my code.
    >I wrote this to try and learn about files and arrays as I thought if I
    >could grab each element and place them into an array I can manipulate
    >the stings from the file with array indexes.
    >Perhaps there's a better method but I'm learning.
    >
    >#include <stdio.h>
    >#include <stdlib.h>
    >
    >int main()
    >{
    > char c;


    The return value from getc needs to be an int, not a char.

    > int num=0;
    > int count=0;
    > int i;
    > char instring[10];
    >
    > FILE * fp;
    >
    > if((fp=fopen("testdata.txt","r"))==NULL)
    > {
    > printf("Error in openning the file.\n");
    > exit(2);


    2 is not a portable return value from main. Use EXIT_FAILURE which is
    a macro that each system defines consistent with your environment.

    > }
    >
    > while(!feof(fp))


    Since you test for EOF inside the loop, calling the function here is
    superfluous.

    > {
    > num++;
    > if ((c = getc(fp))==EOF)


    It is not guaranteed that the char c can hold the value EOF. It needs
    to be an int.

    > {
    > printf("EOF reached \n");
    > break;
    > }
    > else
    > {
    > if (c != '\0')


    In a text file, it is unlikely that c will ever equal '\0''\.

    > {
    > instring[count] =c;
    > printf("count = %d, instring = %c\n",count,instring[count]);


    The fact that c was not equal to EOF or '\0' does not mean it is a
    printable character. It could have been '\n' indicating end of line
    (not end of file) or other common text markers such as '\t'.

    > count++;
    > }
    > }
    > }


    The most likely cause of your segmentation faults is not insuring that
    count never exceeds 9. Once it does and you attempt to store or print
    instring[10], you have exceeded the array bounds and invoked undefined
    behavior.

    >
    > i=0;
    > printf("char 3 is %c\n",instring[i+3]);
    >
    > return 0;
    >}
    >
    >The testdata.txt contains
    >ab$
    >e.f
    >1we
    >@\/
    >"@'
    >:]_
    >.
    >
    >When I execute the program I get the following result:
    >count = 0, instring = a
    >count = 1, instring = b
    >count = 2, instring = $
    >count = 3, instring =
    >


    This is what happens when you attempt to printf a '\n' with %c.

    >count = 4, instring = e
    >count = 5, instring = .
    >count = 6, instring = f
    >count = 7, instring =
    >
    >count = 8, instring = 1
    >count = 9, instring = w
    >count = 10, instring = e


    Of course this element does not exist and everything from this point
    on is undefined behavior.

    >count = 11, instring =
    >
    >count = 12, instring = @
    >count = 13, instring = \
    >count = 14, instring = /
    >count = 15, instring =
    >
    >count = 16, instring = "
    >count = 17, instring = @
    >count = 18, instring = '
    >count = 19, instring =
    >
    >Segmentation Fault (core dumped)
    >
    >My question is if the instring has 10 elements then why is it printing
    >out to element 19 ?


    Because the c language does not require bounds checking during
    run-time. It was a design decision by the language developers to
    leave this task to the programmer. Since you didn't, you experienced
    to different possible manifestations of undefined behavior. The first
    ten times it did something "almost reasonable". The next time it did
    something so unreasonable your operating system had to intercede and
    terminate your program.

    >If I put a large number, such as char instring[150], then it will work


    It will work longer but if your file has more than 150 characters you
    will end up with the same problem. What you really need to do is add
    code to your while loop to check the value of count and do something
    special (terminate with a warning message, reset count back to 0, etc)
    when it exceeds the size of instring.

    >
    >Have I misunderstood arrays completely ?


    What did you expect to happen when you exceeded the array size?


    Remove del for email
    Barry Schwarz, May 19, 2007
    #6
  7. Michael Guest

    On May 19, 12:23 am, Chris Dollin <> wrote:
    > Michael wrote:
    > > Ian answered your actual question, so let me point out another problem
    > > in your code:

    >
    > >> char c;

    > > ...
    > >> if ((c = getc(fp))==EOF)

    >
    > > The function getc returns an integer. I know, that's crazy, but
    > > that's the way it is.

    >
    > Suppose `getc` returned a `char`. What `char` value would you
    > pick to represent EOF and hence disallow from appearing in any
    > FILE* input?
    >
    > Remember that FILE*s can be reading "binary" files.
    >
    > --


    Alternately, suppose you wrote a function where the description is
    "read the next thing and return an int, not a char." Would you call
    such a function getc?

    Or suppose your description of your function was: sometimes return one
    thing, sometimes return another (e.g., return a char except if you
    don't, because you've reached end of file, or if an error occurs).
    Would you think that, perhaps, a better interface was in order?

    Or suppose you developed an interface that was commonly misused, and
    in fact, was incredibly easy to misuse, because you had a combination
    of: 1) outputs that mean different things, 2) a bad name, and 3) a
    common misuse pattern that can not be caught by the compiler. If you
    were the one to develop such an interface, I would blame you, not the
    poor schmucks who were confused by your inanity. Although I suppose
    that the typical thing is design such goofiness, document it, and them
    blame the person who uses the interface for not reading the
    documentation closely enough, regardless of how poorly the interface
    is designed.

    </rant>

    Michael
    Michael, May 20, 2007
    #7
  8. Guest

    Thank you for everyone's reply.
    I've followed through with the suggestions and it's now working.
    I think I would be better off by drawing up a diagram of my logic
    first next time.
    , May 20, 2007
    #8
  9. Guest

    On May 19, 10:43 am, Ian Collins <> wrote:
    > wrote:
    > > Hi, I'm getting confused with arrays and hope someone can shed light
    > > on my code.
    > > I wrote this to try and learn about files and arrays as I thought if I
    > > could grab each element and place them into an array I can manipulate
    > > the stings from the file with array indexes.
    > > Perhaps there's a better method but I'm learning.

    >
    > > #include <stdio.h>
    > > #include <stdlib.h>

    >
    > > int main()
    > > {
    > > char c;
    > > int num=0;
    > > int count=0;
    > > int i;
    > > char instring[10];

    >
    > > FILE * fp;

    >
    > > if((fp=fopen("testdata.txt","r"))==NULL)
    > > {
    > > printf("Error in openning the file.\n");
    > > exit(2);
    > > }

    >
    > > while(!feof(fp))
    > > {
    > > num++;
    > > if ((c = getc(fp))==EOF)
    > > {
    > > printf("EOF reached \n");
    > > break;
    > > }
    > > else
    > > {
    > > if (c != '\0')
    > > {
    > > instring[count] =c;
    > > printf("count = %d, instring = %c\n",count,instring[count]);
    > > count++;
    > > }
    > > }
    > > }

    >
    > > i=0;
    > > printf("char 3 is %c\n",instring[i+3]);

    >
    > > return 0;
    > > }

    >
    > > My question is if the instring has 10 elements then why is it printing
    > > out to element 19 ?
    > > If I put a large number, such as char instring[150], then it will work

    >
    > > Have I misunderstood arrays completely ?

    >
    > You have an array of single characters, which you then fill, and keep
    > writing to without checking if you have written past the end. It looks
    > like you intended to read lines, rather than single characters.
    >
    > --
    > Ian Collins.


    HAI EVERYBODY. I AM NEW TO C. EVERYBODY ANSWERED TO QUESTION, BUT
    NOBODY IS POINTING WHY HE/SHE HAS CHECKED FOR EOF TWICE IN THE
    PROGRAM. IS IT NECESSARY????
    , May 20, 2007
    #9
  10. Army1987 Guest

    "Michael" <> ha scritto nel messaggio
    news:...
    > On May 19, 12:23 am, Chris Dollin <> wrote:
    >> Michael wrote:
    >> > Ian answered your actual question, so let me point out another problem
    >> > in your code:

    >>
    >> >> char c;
    >> > ...
    >> >> if ((c = getc(fp))==EOF)

    >>
    >> > The function getc returns an integer. I know, that's crazy, but
    >> > that's the way it is.

    >>
    >> Suppose `getc` returned a `char`. What `char` value would you
    >> pick to represent EOF and hence disallow from appearing in any
    >> FILE* input?
    >>
    >> Remember that FILE*s can be reading "binary" files.
    >>
    >> --

    >
    > Alternately, suppose you wrote a function where the description is
    > "read the next thing and return an int, not a char." Would you call
    > such a function getc?


    If you want to use fscanf(fp, "%c", &c) or fread(&c, 1, 1, fp) you
    are free to use them. If you would like a function returning a
    char, there would be no way to report EOF, unless it looked like
    extern unsigned char get(FILE * restrict stream, int *flag), which
    IMO is no improvement. So what's your point?
    Army1987, May 20, 2007
    #10
  11. Army1987 Guest

    "Barry Schwarz" <> ha scritto nel messaggio
    news:...
    > On 18 May 2007 22:28:50 -0700, wrote:


    >>When I execute the program I get the following result:
    >>count = 0, instring = a
    >>count = 1, instring = b
    >>count = 2, instring = $
    >>count = 3, instring =
    >>

    >
    > This is what happens when you attempt to printf a '\n' with %c.


    What do you mean? printf("%c", '\n') is perfectly valid and does
    exactly what it is supposed to do, as shown above.
    Army1987, May 20, 2007
    #11
  12. Army1987 Guest

    <> ha scritto nel messaggio
    news:...
    > On May 19, 10:43 am, Ian Collins <> wrote:
    >> wrote:
    >> > Hi, I'm getting confused with arrays and hope someone can shed light
    >> > on my code.
    >> > I wrote this to try and learn about files and arrays as I thought if I
    >> > could grab each element and place them into an array I can manipulate
    >> > the stings from the file with array indexes.
    >> > Perhaps there's a better method but I'm learning.

    >>
    >> > #include <stdio.h>
    >> > #include <stdlib.h>

    >>
    >> > int main()
    >> > {
    >> > char c;
    >> > int num=0;
    >> > int count=0;
    >> > int i;
    >> > char instring[10];

    >>
    >> > FILE * fp;

    >>
    >> > if((fp=fopen("testdata.txt","r"))==NULL)
    >> > {
    >> > printf("Error in openning the file.\n");
    >> > exit(2);
    >> > }

    >>
    >> > while(!feof(fp))
    >> > {
    >> > num++;
    >> > if ((c = getc(fp))==EOF)
    >> > {
    >> > printf("EOF reached \n");
    >> > break;
    >> > }
    >> > else
    >> > {
    >> > if (c != '\0')
    >> > {
    >> > instring[count] =c;
    >> > printf("count = %d, instring = %c\n",count,instring[count]);
    >> > count++;
    >> > }
    >> > }
    >> > }

    >>
    >> > i=0;
    >> > printf("char 3 is %c\n",instring[i+3]);

    >>
    >> > return 0;
    >> > }

    >>
    >> > My question is if the instring has 10 elements then why is it printing
    >> > out to element 19 ?
    >> > If I put a large number, such as char instring[150], then it will work

    >>
    >> > Have I misunderstood arrays completely ?

    >>
    >> You have an array of single characters, which you then fill, and keep
    >> writing to without checking if you have written past the end. It looks
    >> like you intended to read lines, rather than single characters.
    >>
    >> --
    >> Ian Collins.

    >
    > HAI EVERYBODY. I AM NEW TO C. EVERYBODY ANSWERED TO QUESTION, BUT
    > NOBODY IS POINTING WHY HE/SHE HAS CHECKED FOR EOF TWICE IN THE
    > PROGRAM. IS IT NECESSARY????
    >

    No, it isn't.
    They did that because the OP's code did that.
    Army1987, May 20, 2007
    #12
  13. Chris Dollin Guest

    Michael wrote:

    > On May 19, 12:23 am, Chris Dollin <> wrote:
    >> Michael wrote:
    >> > Ian answered your actual question, so let me point out another problem
    >> > in your code:

    >>
    >> >> char c;
    >> > ...
    >> >> if ((c = getc(fp))==EOF)

    >>
    >> > The function getc returns an integer. I know, that's crazy, but
    >> > that's the way it is.

    >>
    >> Suppose `getc` returned a `char`. What `char` value would you
    >> pick to represent EOF and hence disallow from appearing in any
    >> FILE* input?
    >>
    >> Remember that FILE*s can be reading "binary" files.

    >
    > Alternately, suppose you wrote a function where the description is
    > "read the next thing and return an int, not a char." Would you call
    > such a function getc?


    (a) No. But `getc` doesn't return "an int". It returns the int value
    of a char or EOF. Typically it returns a `char`. I'd call it
    `getChar`, and consider giving it return type CharOrEOF, or
    Optional(char), or char??, depending on the language I was
    writing this function in, and its observed conventions.

    (b) I note you didn't answer my question. Either reach-the-next
    char returns a char, and so can't signal EOF in its return value,
    or it returns a non-char, and so can't signal the type of
    the interesting value it returns. Perfection is a vice.

    > Or suppose your description of your function was: sometimes return one
    > thing, sometimes return another (e.g., return a char except if you
    > don't, because you've reached end of file, or if an error occurs).
    > Would you think that, perhaps, a better interface was in order?


    I'd think a better /description/ was in order. Or a better programming
    language.

    > Or suppose you developed an interface that was commonly misused, and
    > in fact, was incredibly easy to misuse, because you had a combination
    > of: 1) outputs that mean different things, 2) a bad name, and 3) a
    > common misuse pattern that can not be caught by the compiler. If you
    > were the one to develop such an interface, I would blame you, not the
    > poor schmucks who were confused by your inanity. Although I suppose
    > that the typical thing is design such goofiness, document it, and them
    > blame the person who uses the interface for not reading the
    > documentation closely enough, regardless of how poorly the interface
    > is designed.


    Were there more room for manoeuver in the existing code, yes. There
    isn't. It's a shame, but there it is.

    > </rant>


    Oh no! The ENTIRE STACK of commentary is unwinding! Gibble ... umph ...

    !gnidniwun si yratnemmoc fo KCATS ERITNE htT !on hO

    (fx:snip)

    --
    Far-Fetched Hedgehog
    The shortcuts are all full of people using them.
    Chris Dollin, May 20, 2007
    #13
  14. On Sun, 20 May 2007 18:45:18 +0200, "Army1987" <>
    wrote:

    >
    >"Barry Schwarz" <> ha scritto nel messaggio
    >news:...
    >> On 18 May 2007 22:28:50 -0700, wrote:

    >
    >>>When I execute the program I get the following result:
    >>>count = 0, instring = a
    >>>count = 1, instring = b
    >>>count = 2, instring = $
    >>>count = 3, instring =
    >>>

    >>
    >> This is what happens when you attempt to printf a '\n' with %c.

    >
    >What do you mean? printf("%c", '\n') is perfectly valid and does
    >exactly what it is supposed to do, as shown above.
    >

    Of course it is.

    Since the OP asked why his output was strange without specifying what
    he found strange other than the segfault, I annotated everything I
    thought a newcomer would be puzzled by.


    Remove del for email
    Barry Schwarz, May 21, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Goh, Yong Kwang
    Replies:
    3
    Views:
    11,784
    Barry Schwarz
    Apr 3, 2004
  2. Sameer
    Replies:
    19
    Views:
    458
  3. esanchezfo

    Segmentation fault char array pointer

    esanchezfo, Jun 1, 2006, in forum: C Programming
    Replies:
    1
    Views:
    434
    Fred Kleinschmidt
    Jun 1, 2006
  4. Ben
    Replies:
    8
    Views:
    457
  5. SP
    Replies:
    8
    Views:
    741
    Dave Thompson
    Aug 14, 2006
Loading...

Share This Page