segmentation error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JK

    segmentation error

    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

  • Richard Heathfield

    #2
    Re: segmentation error

    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

    email: rjh at the above domain, - www.

    Comment

    • JK

      #3
      Re: segmentation error

      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

      Comment

      • Jack Klein

        #4
        Re: segmentation error

        On 6 May 2007 21:14:29 -0700, JK <krishna.januma nchi@gmail.comw rote
        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.l earn.c-c++

        Comment

        • Richard Heathfield

          #5
          Re: segmentation error

          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

          email: rjh at the above domain, - www.

          Comment

          • JK

            #6
            Re: segmentation error

            On May 7, 10:02 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
            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

            Comment

            • Richard Heathfield

              #7
              Re: segmentation error

              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

              email: rjh at the above domain, - www.

              Comment

              • Martin Ambuhl

                #8
                Re: segmentation error

                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.

                Comment

                • pete

                  #9
                  Re: segmentation error

                  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

                  Comment

                  • JK

                    #10
                    Re: segmentation error

                    On May 7, 3:38 pm, pete <pfil...@mindsp ring.comwrote:
                    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

                    Comment

                    • Eric Sosman

                      #11
                      Re: segmentation error

                      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
                      esosman@acm-dot-org.invalid

                      Comment

                      • Richard Heathfield

                        #12
                        Re: segmentation error

                        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

                        email: rjh at the above domain, - www.

                        Comment

                        • Default User

                          #13
                          Re: segmentation error

                          JK wrote:
                          On May 7, 10:02 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                          --
                          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

                          Comment

                          • Keith Thompson

                            #14
                            Re: segmentation error

                            Richard Heathfield <rjh@see.sig.in validwrites:
                            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_Keit h) kst-u@mib.org <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"

                            Comment

                            • Richard

                              #15
                              Re: segmentation error

                              Martin Ambuhl <mambuhl@earthl ink.netwrites:
                              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 ...

                              Comment

                              Working...