segmentation error

Discussion in 'C Programming' started by JK, May 7, 2007.

  1. JK

    JK Guest

    Hi,

    I am getting segmentation error with below-mentioned code and I am not
    able to make out why.

    typedef struct sd {
    int ps;
    int cs;
    } st;

    void main()
    {
    st **fe;
    int n,j;

    n = 11;

    fe = (st **) calloc(n+1, sizeof(st *));
    for (j=0; j<=n; j++)
    fe[j] = (st *) calloc(16, sizeof(st));

    for (j=0; j<=n; j++)
    free(fe[j]);
    free(fe);
    }

    When I tried to debug with gdb, it is pointing to fe[j] = (st *)
    calloc(16, sizeof(st));

    Please suggest.

    Regards,
    JK
    JK, May 7, 2007
    #1
    1. Advertising

  2. JK said:

    > Hi,
    >
    > I am getting segmentation error with below-mentioned code and I am not
    > able to make out why.
    >
    > typedef struct sd {
    > int ps;
    > int cs;
    > } st;
    >
    > void main()


    int main(void)

    Get the entry point right. When you've learned how C programs start,
    we'll be ready for lesson 2.


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 7, 2007
    #2
    1. Advertising

  3. JK

    JK Guest

    Hey Richard, I am sorry if I disturbed you, but just trying to take u
    experts help in fixing up my problem.

    Actually it is a big code and I haven't written it. This part of code
    is inside a function which is being called in main function.
    Please bear with me - I am not a c programmer. I am from VHDL
    programming side and this is a overtime work for me...

    The problem is when I run my RTL regressions, I need to call this c
    program exe for every testcase. At a random point (testcase 46), this
    c program output crashes.
    When I tried to debug with gdb, it pointed to above mentioned line.
    Instead of wasting your time, I just tried to show - the problematic
    piece of code.

    typedef struct sd {
    int ps;
    int cs;
    } st;

    int main()
    {
    st **fe;
    int n,j;

    n = 11;

    fe = (st **) calloc(n+1, sizeof(st *));
    for (j=0; j<=n; j++)
    fe[j] = (st *) calloc(16, sizeof(st));

    for (j=0; j<=n; j++)
    free(fe[j]);
    free(fe);

    return 0;
    }

    regards,
    JK
    JK, May 7, 2007
    #3
  4. JK

    Jack Klein Guest

    On 6 May 2007 21:14:29 -0700, JK <> wrote
    in comp.lang.c:

    > Hi,
    >
    > I am getting segmentation error with below-mentioned code and I am not
    > able to make out why.
    >
    > typedef struct sd {
    > int ps;
    > int cs;
    > } st;
    >
    > void main()


    As Richard said, your program has undefined behavior because of the
    incorrect definition of main(). C does not know or care what happens
    with your program.

    > {
    > st **fe;
    > int n,j;
    >
    > n = 11;
    >
    > fe = (st **) calloc(n+1, sizeof(st *));


    Remove the casts on the pointer returned by calloc(). If your
    compiler complains without the casts, see if you can figure out why.

    > for (j=0; j<=n; j++)
    > fe[j] = (st *) calloc(16, sizeof(st));
    >
    > for (j=0; j<=n; j++)
    > free(fe[j]);
    > free(fe);
    > }
    >
    > When I tried to debug with gdb, it is pointing to fe[j] = (st *)
    > calloc(16, sizeof(st));
    >
    > Please suggest.
    >
    > Regards,
    > JK


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, May 7, 2007
    #4
  5. JK said:

    > Hey Richard, I am sorry if I disturbed you,


    You didn't.

    > but just trying to take u experts help in fixing up my problem.


    Great!

    > Please bear with me - I am not a c programmer.


    Consider hiring one.

    > typedef struct sd {
    > int ps;
    > int cs;
    > } st;
    >
    > int main()
    > {
    > st **fe;
    > int n,j;
    >
    > n = 11;
    >
    > fe = (st **) calloc(n+1, sizeof(st *));


    Here is your second problem, which you have managed to obscure with a
    pointless cast. When you rewrite it like this:

    fe = calloc(n + 1, sizeof *fe);

    and re-compile, you will discover that the compiler issues a diagnostic
    message. Why? Well, the compiler will not be able to find a prototype
    for calloc, so it will make a default assumption (as the Standard
    requires) about the return value of the calloc function, and that
    assumption is unfortunate where calloc is concerned. This error is
    happening anyway, but your cast conceals it.

    You must fix the real problem by telling the compiler the true return
    type of calloc, which you do by adding this line at the top of your
    program:

    #include <stdlib.h>

    Also, you mustn't just assume that the memory request succeeded. If
    calloc could not allocate the memory you wanted, it will return NULL.
    Test for this.

    > for (j=0; j<=n; j++)
    > fe[j] = (st *) calloc(16, sizeof(st));


    Same applies here. Also note that you allocated space for twelve (n is
    11, and you asked for n + 1) objects, but here you try to access
    thirteen (indices 0 through 12, count them). Hence your segfault.


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 7, 2007
    #5
  6. JK

    JK Guest

    On May 7, 10:02 am, Richard Heathfield <> wrote:
    > JK said:
    >
    > > Hey Richard, I am sorry if I disturbed you,

    >
    > You didn't.
    >
    > > but just trying to take u experts help in fixing up my problem.

    >
    > Great!
    >
    > > Please bear with me - I am not a c programmer.

    >
    > Consider hiring one.
    >
    > > typedef struct sd {
    > > int ps;
    > > int cs;
    > > } st;

    >
    > > int main()
    > > {
    > > st **fe;
    > > int n,j;

    >
    > > n = 11;

    >
    > > fe = (st **) calloc(n+1, sizeof(st *));

    >
    > Here is your second problem, which you have managed to obscure with a
    > pointless cast. When you rewrite it like this:
    >
    > fe = calloc(n + 1, sizeof *fe);
    >
    > and re-compile, you will discover that the compiler issues a diagnostic
    > message. Why? Well, the compiler will not be able to find a prototype
    > for calloc, so it will make a default assumption (as the Standard
    > requires) about the return value of the calloc function, and that
    > assumption is unfortunate where calloc is concerned. This error is
    > happening anyway, but your cast conceals it.
    >
    > You must fix the real problem by telling the compiler the true return
    > type of calloc, which you do by adding this line at the top of your
    > program:
    >
    > #include <stdlib.h>
    >
    > Also, you mustn't just assume that the memory request succeeded. If
    > calloc could not allocate the memory you wanted, it will return NULL.
    > Test for this.
    >
    > > for (j=0; j<=n; j++)
    > > fe[j] = (st *) calloc(16, sizeof(st));

    >
    > Same applies here. Also note that you allocated space for twelve (n is
    > 11, and you asked for n + 1) objects, but here you try to access
    > thirteen (indices 0 through 12, count them). Hence your segfault.
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
    > email: rjh at the above domain, - www.


    Richard,

    > #include <stdlib.h>

    stdlib.h, math.h, stdio.h are included.

    > > for (j=0; j<=n; j++)
    > > fe[j] = (st *) calloc(16, sizeof(st));

    >
    > Same applies here. Also note that you allocated space for twelve (n is
    > 11, and you asked for n + 1) objects, but here you try to access
    > thirteen (indices 0 through 12, count them). Hence your segfault.


    I didn't get this point. How I am trying to access thirteen??? indices
    are 0 to 11, so it will be 12 only...

    Regards,
    JK
    JK, May 7, 2007
    #6
  7. JK said:

    <snip>

    > stdlib.h, math.h, stdio.h are included.


    We are not mind-readers. When asking for help, please post the smallest
    COMPLETE program that demonstrates the problem you are having.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 7, 2007
    #7
  8. JK wrote:
    > Hi,
    >
    > I am getting segmentation error with below-mentioned code and I am not
    > able to make out why.
    >
    > typedef struct sd {
    > int ps;
    > int cs;
    > } st;
    >
    > void main()

    ^^^^
    You've already lost. Anything after this is irrelevant.
    Martin Ambuhl, May 7, 2007
    #8
  9. JK

    pete Guest

    JK wrote:
    >
    > Hi,
    >
    > I am getting segmentation error with below-mentioned code and I am not
    > able to make out why.
    >
    > typedef struct sd {
    > int ps;
    > int cs;
    > } st;
    >
    > void main()
    > {
    > st **fe;
    > int n,j;
    >
    > n = 11;
    >
    > fe = (st **) calloc(n+1, sizeof(st *));
    > for (j=0; j<=n; j++)
    > fe[j] = (st *) calloc(16, sizeof(st));
    >
    > for (j=0; j<=n; j++)
    > free(fe[j]);
    > free(fe);
    > }
    >
    > When I tried to debug with gdb, it is pointing to fe[j] = (st *)
    > calloc(16, sizeof(st));
    >
    > Please suggest.


    /* BEGIN new.c */

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

    typedef struct sd {
    int ps;
    int cs;
    } st;

    int main(void)
    {
    st **fe;
    int n, j;

    n = 11;
    fe = calloc(n + 1, sizeof *fe);
    if (fe != NULL) {
    for (j = 0; j <= n; j++) {
    fe[j] = calloc(16, sizeof *fe[j]);
    if (fe[j] == NULL) {
    printf("fe[%d] is equal to NULL\n", j);
    while (j-- != 0) {
    free(fe[j]);
    }
    free(fe);
    fe = NULL;
    }
    }
    }
    if (fe != NULL) {
    for (j = 0; j <= n; j++) {
    free(fe[j]);
    }
    free(fe);
    }
    puts("teh enb!");
    return 0;
    }

    /* END new.c */


    --
    pete
    pete, May 7, 2007
    #9
  10. JK

    JK Guest

    On May 7, 3:38 pm, pete <> wrote:
    > JK wrote:
    >
    > > Hi,

    >
    > > I am getting segmentation error with below-mentioned code and I am not
    > > able to make out why.

    >
    > > typedef struct sd {
    > > int ps;
    > > int cs;
    > > } st;

    >
    > > void main()
    > > {
    > > st **fe;
    > > int n,j;

    >
    > > n = 11;

    >
    > > fe = (st **) calloc(n+1, sizeof(st *));
    > > for (j=0; j<=n; j++)
    > > fe[j] = (st *) calloc(16, sizeof(st));

    >
    > > for (j=0; j<=n; j++)
    > > free(fe[j]);
    > > free(fe);
    > > }

    >
    > > When I tried to debug with gdb, it is pointing to fe[j] = (st *)
    > > calloc(16, sizeof(st));

    >
    > > Please suggest.

    >
    > /* BEGIN new.c */
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    >
    > typedef struct sd {
    > int ps;
    > int cs;
    >
    > } st;
    >
    > int main(void)
    > {
    > st **fe;
    > int n, j;
    >
    > n = 11;
    > fe = calloc(n + 1, sizeof *fe);
    > if (fe != NULL) {
    > for (j = 0; j <= n; j++) {
    > fe[j] = calloc(16, sizeof *fe[j]);
    > if (fe[j] == NULL) {
    > printf("fe[%d] is equal to NULL\n", j);
    > while (j-- != 0) {
    > free(fe[j]);
    > }
    > free(fe);
    > fe = NULL;
    > }
    > }
    > }
    > if (fe != NULL) {
    > for (j = 0; j <= n; j++) {
    > free(fe[j]);
    > }
    > free(fe);
    > }
    > puts("teh enb!");
    > return 0;
    >
    > }
    >
    > /* END new.c */
    >
    > --
    > pete- Hide quoted text -
    >
    > - Show quoted text -


    Thanx Pete.

    Regards,
    JK
    JK, May 7, 2007
    #10
  11. JK

    Eric Sosman Guest

    Richard Heathfield wrote:
    > JK said:
    >>
    >> for (j=0; j<=n; j++)
    >> fe[j] = (st *) calloc(16, sizeof(st));

    >
    > Same applies here. Also note that you allocated space for twelve (n is
    > 11, and you asked for n + 1) objects, but here you try to access
    > thirteen (indices 0 through 12, count them). Hence your segfault.


    Where is the use of index [12]?

    (It seems to me the fault most likely stems from the
    failure to include <stdlib.h>, as you noted earlier.)

    --
    Eric Sosman
    lid
    Eric Sosman, May 7, 2007
    #11
  12. Eric Sosman said:

    > Richard Heathfield wrote:
    >> JK said:
    >>>
    >>> for (j=0; j<=n; j++)
    >>> fe[j] = (st *) calloc(16, sizeof(st));

    >>
    >> Same applies here. Also note that you allocated space for twelve (n
    >> is 11, and you asked for n + 1) objects, but here you try to access
    >> thirteen (indices 0 through 12, count them). Hence your segfault.

    >
    > Where is the use of index [12]?


    In my fetid imagination, it appears. My apologies to the OP.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 7, 2007
    #12
  13. JK

    Default User Guest

    JK wrote:

    > On May 7, 10:02 am, Richard Heathfield <> wrote:


    > > --
    > > Richard Heathfield
    > > "Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
    > > email: rjh at the above domain, - www.

    >
    > Richard,



    Please trim your replies to the minimum needed for context. In
    particular, remove signature blocks from the replies (Google won't do
    it automatically for you the way most newsreaders do).




    Brian
    Default User, May 7, 2007
    #13
  14. Richard Heathfield <> writes:
    > Eric Sosman said:
    >
    >> Richard Heathfield wrote:
    >>> JK said:
    >>>>
    >>>> for (j=0; j<=n; j++)
    >>>> fe[j] = (st *) calloc(16, sizeof(st));
    >>>
    >>> Same applies here. Also note that you allocated space for twelve (n
    >>> is 11, and you asked for n + 1) objects, but here you try to access
    >>> thirteen (indices 0 through 12, count them). Hence your segfault.

    >>
    >> Where is the use of index [12]?

    >
    > In my fetid imagination, it appears. My apologies to the OP.


    In Richard's defense, the OP's code is a bit misleading here. The
    usual idiom in C is to keep track of the number of elements in an
    array rather than the index of the last element. For example (using a
    constant rather than a variable):

    #define N 10
    int arr[N];

    for (j = 0; j < N; j ++) { ... }

    The use of "<=" in a for loop, as above:

    for (j=0; j<=n; j++)
    ...

    is often a sign that the author *should* have used "<" rather than
    "<=", and is going past the end of the array. There's nothing
    incorrect in the original code's handling of the array indices (as far
    as I know, I haven't studied it in detail), but it would have been
    clearer and more idiomatic to set n to 12 and use:

    for (j = 0; j < n; j ++)
    ...

    and perhaps also to give "n" a name that indicates more clearly what
    it represents.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 7, 2007
    #14
  15. JK

    Richard Guest

    Martin Ambuhl <> writes:

    > JK wrote:
    >> Hi,
    >>
    >> I am getting segmentation error with below-mentioned code and I am not
    >> able to make out why.
    >>
    >> typedef struct sd {
    >> int ps;
    >> int cs;
    >> } st;
    >>
    >> void main()

    > ^^^^
    > You've already lost. Anything after this is irrelevant.


    You must be a bundle of fun to work with. But coming back into the real
    world ...
    Richard, May 7, 2007
    #15
  16. In article <>, Richard <> wrote:
    >Martin Ambuhl <> writes:
    >
    >> JK wrote:


    >>> void main()

    >> ^^^^
    >> You've already lost. Anything after this is irrelevant.

    >
    >You must be a bundle of fun to work with. But coming back into the real
    >world ...


    In the real world, getting the basics right is the bare minimum for
    being taken seriously.


    dave

    --
    Dave Vandervies
    That is why I said "if you view your computer as a big finite state
    machine" -- that eliminiates aerial nasal daemons, which are infinite,
    stateless, and animate. --Chris Torek in comp.lang.c
    Dave Vandervies, May 7, 2007
    #16
  17. Richard wrote:
    > Martin Ambuhl <> writes:
    >
    >> JK wrote:
    >>> Hi,
    >>>
    >>> I am getting segmentation error with below-mentioned code and I am not
    >>> able to make out why.
    >>>
    >>> typedef struct sd {
    >>> int ps;
    >>> int cs;
    >>> } st;
    >>>
    >>> void main()

    >> ^^^^
    >> You've already lost. Anything after this is irrelevant.

    >
    > You must be a bundle of fun to work with. But coming back into the real
    > world ...


    In the real world, "void main()" insures that you don't work anywhere
    very long, so no one knows what kind of other mad antics you pull to
    make yourself "a bundle of fun."
    Martin Ambuhl, May 7, 2007
    #17
  18. Martin Ambuhl <> writes:
    > Richard wrote:
    >> Martin Ambuhl <> writes:
    >>> JK wrote:
    >>>> I am getting segmentation error with below-mentioned code and I am not
    >>>> able to make out why.
    >>>>
    >>>> typedef struct sd {
    >>>> int ps;
    >>>> int cs;
    >>>> } st;
    >>>>
    >>>> void main()
    >>> ^^^^
    >>> You've already lost. Anything after this is irrelevant.

    >> You must be a bundle of fun to work with. But coming back into the
    >> real
    >> world ...

    >
    > In the real world, "void main()" insures that you don't work anywhere
    > very long, so no one knows what kind of other mad antics you pull to
    > make yourself "a bundle of fun."


    In the real world, unfortunately, I doubt that that's true. It's
    certainly not the case that "void main()" prevents one from making a
    living as an author of books about C.

    On the other hand, although "void main()" invokes undefined behavior
    (assuming a hosted implementation, unless the implementation
    specifically documents it as an extension), most real-world
    implementations will either reject it outright *or* treat it as nearly
    equivalent to the correct "int main(void)" (possibly with a warning).
    I've never seen a compiler do anything else, though of course my
    experience is far from universal.

    Given the lamentable existence of books that endorce "void main()",
    and given that many implementations do not reject it, I generally
    prefer to point out that it's an error *and* to go on and comment on
    the rest of the code. If a program uses "void main()" and misbehaves,
    there's probably some *other* reason for it.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 8, 2007
    #18
  19. Keith Thompson said:

    <snip>

    > Given the lamentable existence of books that endorce "void main()",
    > and given that many implementations do not reject it, I generally
    > prefer to point out that it's an error *and* to go on and comment on
    > the rest of the code. If a program uses "void main()" and misbehaves,
    > there's probably some *other* reason for it.


    Unless it's a VAX, of course. Anyway, the way I see it, until people can
    grok something as simple as main's return type, there is little point
    in trying to teach them anything else. That is why I am getting into
    the habit of either ignoring void main posts completely, or pointing
    out just that one error and leaving it there.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 8, 2007
    #19
  20. JK

    Eric Sosman Guest

    Keith Thompson wrote:
    > Richard Heathfield <> writes:
    >> Eric Sosman said:
    >>
    >>> Richard Heathfield wrote:
    >>>> JK said:
    >>>>> for (j=0; j<=n; j++)
    >>>>> fe[j] = (st *) calloc(16, sizeof(st));
    >>>> Same applies here. Also note that you allocated space for twelve (n
    >>>> is 11, and you asked for n + 1) objects, but here you try to access
    >>>> thirteen (indices 0 through 12, count them). Hence your segfault.
    >>> Where is the use of index [12]?

    >> In my fetid imagination, it appears. My apologies to the OP.

    >
    > In Richard's defense, the OP's code is a bit misleading here. The
    > usual idiom in C is to keep track of the number of elements in an
    > array rather than the index of the last element. [...]


    Confession: I, too, said "Bug! Bug! Bug!" (cue sound
    track from the shower murder scene in "Psycho") as soon as I
    saw the O.P.'s `<=', but (for a change) caught my mistake
    before posting.

    There may be dual lessons about patterns here: One, that
    they help us to write correct code because we've debugged the
    patterns themselves and needn't reconsider them, but Two, that
    we are prone to misconstrue a program that departs from the
    pattern. We see `malloc(strlen(s))' and think "Bug! Bug! Bug!"
    and only later notice that the allocated area is not being used
    to hold a copy of the string. We see `isalpha(*str)' and think
    "Bug! Bug! Bug!" until we see that `str' is an `unsigned char*'.
    We see `defualt:' and think "Bug! Bug! Bug!" until we find the
    corresponding `goto defualt;' and instead turn our attention to
    the author of the code: "Bastard! Bastard! Bastard!"

    Patterns are useful, patterns are powerful, but patterns can
    also deceive. "Christ! What are patterns for?"

    --
    Eric Sosman
    lid
    Eric Sosman, May 8, 2007
    #20
    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. Oleg
    Replies:
    9
    Views:
    12,350
  2. CBFalconer

    Re: fclose cause segmentation error

    CBFalconer, Feb 17, 2005, in forum: C Programming
    Replies:
    2
    Views:
    358
    Kenneth Brody
    Feb 17, 2005
  3. Rob Morris

    Re: fclose cause segmentation error

    Rob Morris, Feb 17, 2005, in forum: C Programming
    Replies:
    0
    Views:
    506
    Rob Morris
    Feb 17, 2005
  4. Rufus V. Smith

    Re: fclose cause segmentation error

    Rufus V. Smith, Feb 17, 2005, in forum: C Programming
    Replies:
    0
    Views:
    363
    Rufus V. Smith
    Feb 17, 2005
  5. questions?

    what's reason to have a segmentation error?

    questions?, Feb 27, 2006, in forum: C Programming
    Replies:
    9
    Views:
    716
    Dave Thompson
    Mar 6, 2006
Loading...

Share This Page