Segmentation Fault

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

    #1

    Segmentation Fault

    Hi all,

    can anybody help me with the following code:

    #include <stdio.h>
    #include <emmintrin.h>
    #define M 2
    int main () {
    int i;
    __m128d *s, ts, td;

    double *p = malloc(M*sizeof (*p)); //A
    // double p[2]; //B

    if (p==NULL) return 0;

    p[0]=p[1]=1.0;

    printf("%.3f %.3f\n", p[0], p[1]);

    s = (__m128d *)p; //D
    if (s != p) return 0;
    ts = *s;
    td = _mm_add_pd(ts, ts);
    *s = td; //C

    printf("%.3f %.3f\n", p[0], p[1]);
    }

    Line "C" causes a Segmentation Fault and I don't know why. If I replace line
    "A" with line "B" it works correctly. Something must be wrong with line "D"
    maybe somebody knows what?

    Thanks for any help,
    ChL

    PS: I'm using an Intel Compiler icc version 8.0 on a Xeon machine compiling
    with the command "icc test.c -o test -march=pentium4" .


  • Mark A. Odell

    #2
    Re: Segmentation Fault

    "Christian Lande" <IIZUXMKQOEHH@s pammotel.com> wrote in
    news:cb9lot$fn2 $04$1@news.t-online.com:
    [color=blue]
    > Hi all,
    >
    > can anybody help me with the following code:
    >
    > #include <stdio.h>
    > #include <emmintrin.h>
    > #define M 2
    > int main () {
    > int i;
    > __m128d *s, ts, td;
    >
    > double *p = malloc(M*sizeof (*p)); //A
    > // double p[2]; //B
    >
    > if (p==NULL) return 0;
    >
    > p[0]=p[1]=1.0;
    >
    > printf("%.3f %.3f\n", p[0], p[1]);
    >
    > s = (__m128d *)p; //D[/color]

    What the heck is __m128d? Hint: it's not ISO C so how can we help you
    here?


    --
    - Mark ->
    --

    Comment

    • Christian Lande

      #3
      Re: Segmentation Fault

      "Mark A. Odell" <odellmark@hotm ail.com> wrote:
      [color=blue]
      > What the heck is __m128d? Hint: it's not ISO C so how can we help you
      > here?[/color]

      yes, you are right, it's not ISO C, sorry. But I don't know any other
      position where to ask such a question and I'm sure here are some people who
      know the answer.

      If you are interested in what is __m128d, you can look here
      http://msdn.microsoft.com/library/de...lrf__m128d.asp
      e.g. Some other compilers (like Intel icc for linux and windows) implement
      it in the same way.

      Thx for your answer,
      ChL


      Comment

      • Darrell Grainger

        #4
        Re: Segmentation Fault

        On Tue, 22 Jun 2004, Christian Lande wrote:
        [color=blue]
        > Hi all,
        >
        > can anybody help me with the following code:
        >
        > #include <stdio.h>
        > #include <emmintrin.h>[/color]

        Have no idea what this header is. Without an explanation this could have
        some impact.
        [color=blue]
        > #define M 2
        > int main () {
        > int i;
        > __m128d *s, ts, td;[/color]

        What is __m128d? This is not a standard data type.
        [color=blue]
        > double *p = malloc(M*sizeof (*p)); //A[/color]

        First use of malloc without prototype. You need to add an #include
        <stdlib.h> to your program.
        [color=blue]
        > // double p[2]; //B
        >
        > if (p==NULL) return 0;
        >
        > p[0]=p[1]=1.0;
        >
        > printf("%.3f %.3f\n", p[0], p[1]);
        >
        > s = (__m128d *)p; //D[/color]

        Here I still have no idea what __m128d is and how it will affect things.
        [color=blue]
        > if (s != p) return 0;[/color]

        This tells me that the implicit conversion and explicit conversion must be
        the same to get passed this point. Is sizeof *p == sizeof *s? If not then
        what is going on here?
        [color=blue]
        > ts = *s;
        > td = _mm_add_pd(ts, ts);
        > *s = td; //C[/color]

        Is this definitely C? The use of C++ style comments makes me wonder. If it
        is C, sizeof *p < sizeof *s could cause a crash here. Or are you assuming
        that sizeof p[0] + sizeof p[1] == sizeof *s?

        If that is the case, why not use a union that contains pointers to
        doubles and a pointer to a __m128d. It would be more clear and the
        compiler might catch something you are missing.
        [color=blue]
        > printf("%.3f %.3f\n", p[0], p[1]);[/color]

        P.S. if main returns an int, where is your return statement?
        [color=blue]
        > }
        >
        > Line "C" causes a Segmentation Fault and I don't know why. If I replace line
        > "A" with line "B" it works correctly. Something must be wrong with line "D"
        > maybe somebody knows what?
        >
        > Thanks for any help,
        > ChL
        >
        > PS: I'm using an Intel Compiler icc version 8.0 on a Xeon machine compiling
        > with the command "icc test.c -o test -march=pentium4" .
        >
        >
        >[/color]

        --
        Send e-mail to: darrell at cs dot toronto dot edu
        Don't send e-mail to vice.president@ whitehouse.gov

        Comment

        • Irrwahn Grausewitz

          #5
          Re: Segmentation Fault

          "Christian Lande" <IIZUXMKQOEHH@s pammotel.com> wrote:[color=blue]
          >Hi all,
          >
          >can anybody help me with the following code:
          >
          >#include <stdio.h>
          >#include <emmintrin.h>[/color]

          As Mark already pointed out, by including this header you left
          the realm of portable standard C.
          [color=blue]
          >#define M 2
          >int main () {
          > int i;
          > __m128d *s, ts, td;
          >
          > double *p = malloc(M*sizeof (*p)); //A[/color]

          You forgot to #include <stdlib.h> before calling malloc; this might
          well be the reason for the problem.
          [color=blue]
          >// double p[2]; //B
          >
          > if (p==NULL) return 0;
          >
          > p[0]=p[1]=1.0;
          >
          > printf("%.3f %.3f\n", p[0], p[1]);
          >
          > s = (__m128d *)p; //D
          > if (s != p) return 0;
          > ts = *s;
          > td = _mm_add_pd(ts, ts);
          > *s = td; //C
          >
          > printf("%.3f %.3f\n", p[0], p[1]);
          >}
          >
          >Line "C" causes a Segmentation Fault and I don't know why.[/color]

          If including stdlib.h didn't fix the code, here's another possible
          cause for the segfault: if an object of type __m128d (whatever that
          is) requires more memory than p points to, you write to memory your
          program doesn't own in line "C".

          <snip>

          Regards
          --
          Irrwahn Grausewitz (irrwahn33@free net.de)
          welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
          clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
          clc OT guide : http://benpfaff.org/writings/clc/off-topic.html

          Comment

          • Dan Pop

            #6
            Re: Segmentation Fault

            In <cb9lot$fn2$04$ 1@news.t-online.com> "Christian Lande" <IIZUXMKQOEHH@s pammotel.com> writes:
            [color=blue]
            >#include <stdio.h>
            >#include <emmintrin.h>[/color]

            ???
            [color=blue]
            >#define M 2
            >int main () {
            > int i;
            > __m128d *s, ts, td;[/color]

            I assume __m128d is an 128-bit (16 byte) type.
            [color=blue]
            > double *p = malloc(M*sizeof (*p)); //A
            >// double p[2]; //B
            >
            > if (p==NULL) return 0;
            >
            > p[0]=p[1]=1.0;
            >
            > printf("%.3f %.3f\n", p[0], p[1]);
            >
            > s = (__m128d *)p; //D
            > if (s != p) return 0;[/color]
            ^^^^^^
            Is the compiler silently accepting this line? It doesn't look like
            __m128d is merely an alias for double and you can't compare pointers to
            different types.
            [color=blue]
            > ts = *s;[/color]

            If this works, then //C should, in principle, work, too.
            [color=blue]
            > td = _mm_add_pd(ts, ts);
            > *s = td; //C[/color]

            The only explanation for a failure here after the success of "ts = *s;"
            is that the _mm_add_pd(ts, ts) call invoked undefined behaviour.
            [color=blue]
            > printf("%.3f %.3f\n", p[0], p[1]);
            >}
            >
            >Line "C" causes a Segmentation Fault and I don't know why. If I replace line
            >"A" with line "B" it works correctly.[/color]

            This replacement generates a different stack layout, so it may be a red
            herring.
            [color=blue]
            >Something must be wrong with line "D" maybe somebody knows what?[/color]

            In this case, "ts = *s;" should have generated the crash. But this
            didn't happen, so the pointer value is OK.
            [color=blue]
            >Thanks for any help,
            >ChL
            >
            >PS: I'm using an Intel Compiler icc version 8.0 on a Xeon machine compiling
            >with the command "icc test.c -o test -march=pentium4" .[/color]

            Your code is not standard C code and your problem has nothing to do
            with standard C.

            Dan
            --
            Dan Pop
            DESY Zeuthen, RZ group
            Email: Dan.Pop@ifh.de

            Comment

            • Christian Lande

              #7
              Re: Segmentation Fault

              Ok, thanks to all of you for reading and trying to help, meanwhile I solved
              the problem.

              It was because of the fact, that this __m128d-type was defined with an
              alignment of every 16 Bytes, so I got the problem because of my reserved
              double field has an alignment of every 8 Bytes.

              Because of your comments that this is not the right group, can you give me a
              hint where to ask for such things, please? These problems occouring from
              time to time and I've allways the same problem not knowing where to ask for.

              Thx,
              ChL


              Comment

              • Dan Pop

                #8
                Re: Segmentation Fault

                In <c7sgd0tr6oensk 7lnnfn5mrfj6es2 fnd6t@4ax.com> Irrwahn Grausewitz <irrwahn33@free net.de> writes:
                [color=blue]
                >"Christian Lande" <IIZUXMKQOEHH@s pammotel.com> wrote:[color=green]
                >>Hi all,
                >>
                >>can anybody help me with the following code:
                >>
                >>#include <stdio.h>
                >>#include <emmintrin.h>[/color]
                >
                >As Mark already pointed out, by including this header you left
                >the realm of portable standard C.
                >[color=green]
                >>#define M 2
                >>int main () {
                >> int i;
                >> __m128d *s, ts, td;
                >>
                >> double *p = malloc(M*sizeof (*p)); //A[/color]
                >
                >You forgot to #include <stdlib.h> before calling malloc; this might
                >well be the reason for the problem.[/color]

                If he did, indeed, the compiler should have complained: a diagnostic is
                required.

                Dan
                --
                Dan Pop
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de

                Comment

                • CBFalconer

                  #9
                  Re: Segmentation Fault

                  Christian Lande wrote:[color=blue]
                  >
                  > can anybody help me with the following code:
                  >
                  > #include <stdio.h>
                  > #include <emmintrin.h>[/color]

                  unknown header.
                  [color=blue]
                  > #define M 2
                  > int main () {[/color]

                  int main (void) is better
                  [color=blue]
                  > int i;
                  > __m128d *s, ts, td;[/color]

                  illegal identifier __m128d. Reserved for implementation
                  [color=blue]
                  >
                  > double *p = malloc(M*sizeof (*p)); //A
                  > // double p[2]; //B[/color]

                  // comments are ill advised in newsgroups, require C99 compiler.
                  You probably don't have one. Use /* ... */ instead.
                  [color=blue]
                  >
                  > if (p==NULL) return 0;
                  >
                  > p[0]=p[1]=1.0;
                  >
                  > printf("%.3f %.3f\n", p[0], p[1]);
                  >
                  > s = (__m128d *)p; //D[/color]

                  Again, unideclared and illegal identifier. Bad comment.
                  [color=blue]
                  > if (s != p) return 0;[/color]

                  Why should it be? They point to different types.
                  [color=blue]
                  > ts = *s;[/color]

                  ts is of type blah, s of type pointer to blah (I won't sink to
                  typing __m128d). Even more things must be undefined by now.
                  [color=blue]
                  > td = _mm_add_pd(ts, ts);[/color]

                  Another illegal identifier, also undefined.
                  [color=blue]
                  > *s = td; //C
                  >
                  > printf("%.3f %.3f\n", p[0], p[1]);
                  > }
                  >
                  > Line "C" causes a Segmentation Fault and I don't know why. If I
                  > replace line "A" with line "B" it works correctly. Something must
                  > be wrong with line "D" maybe somebody knows what?
                  >
                  > Thanks for any help,
                  > ChL
                  >
                  > PS: I'm using an Intel Compiler icc version 8.0 on a Xeon machine
                  > compiling with the command "icc test.c -o test -march=pentium4" .[/color]

                  If that matters you are off topic.

                  --
                  Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                  Available for consulting/temporary embedded and systems.
                  <http://cbfalconer.home .att.net> USE worldnet address!

                  Comment

                  • CBFalconer

                    #10
                    Re: Segmentation Fault

                    Christian Lande wrote:[color=blue]
                    >[/color]
                    .... snip ...[color=blue]
                    >
                    > Because of your comments that this is not the right group, can
                    > you give me a hint where to ask for such things, please? These
                    > problems occouring from time to time and I've allways the same
                    > problem not knowing where to ask for.[/color]

                    Your best bet is to stick to pure ISO standard portable C and
                    eschew system specific extensions. The end convenience will pay
                    for itself many times over.

                    If this is not possible you should, in this case, look for a
                    newsgroup with 'Microsoft' in its name. In that case you should
                    also isolate your non-portable code to a file or two.

                    --
                    Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                    Available for consulting/temporary embedded and systems.
                    <http://cbfalconer.home .att.net> USE worldnet address!


                    Comment

                    • Barry Schwarz

                      #11
                      Re: Segmentation Fault

                      On Tue, 22 Jun 2004 18:13:00 +0200, "Christian Lande"
                      <IIZUXMKQOEHH@s pammotel.com> wrote:
                      [color=blue]
                      >Hi all,
                      >
                      >can anybody help me with the following code:
                      >
                      >#include <stdio.h>
                      >#include <emmintrin.h>
                      >#define M 2
                      >int main () {
                      > int i;
                      > __m128d *s, ts, td;
                      >
                      > double *p = malloc(M*sizeof (*p)); //A
                      >// double p[2]; //B
                      >
                      > if (p==NULL) return 0;
                      >
                      > p[0]=p[1]=1.0;
                      >
                      > printf("%.3f %.3f\n", p[0], p[1]);
                      >
                      > s = (__m128d *)p; //D
                      > if (s != p) return 0;[/color]

                      Doesn't your compiler generate a diagnostic for this? The two pointer
                      types are incompatible unless __m128d is just a typedef for double.
                      [color=blue]
                      > ts = *s;
                      > td = _mm_add_pd(ts, ts);
                      > *s = td; //C[/color]

                      What is the sizeof(__m128d) ? If it is larger than 2*sizeof(double )
                      you are attempting to write to memory you don't own.
                      [color=blue]
                      >
                      > printf("%.3f %.3f\n", p[0], p[1]);
                      >}
                      >
                      >Line "C" causes a Segmentation Fault and I don't know why. If I replace line
                      >"A" with line "B" it works correctly. Something must be wrong with line "D"[/color]

                      If it is the size problem mentioned above, line C will overwrite other
                      variables in your program but at least you own that memory.

                      However, both overwrites invoke undefined behavior so it really won't
                      work even if it appears to.
                      [color=blue]
                      >maybe somebody knows what?[/color]

                      With line A, line D cannot be the problem because the value returned
                      from malloc is guaranteed to be properly aligned for any type,
                      including __m128d. With line B, we don't know if p will be properly
                      aligned.

                      If you really want help, you are going to have to give us all the
                      non-standard details. What is type __m128d? What does _mm_add_pd do?


                      <<Remove the del for email>>

                      Comment

                      • Ulrich Eckhardt

                        #12
                        Re: Segmentation Fault

                        Christian Lande wrote:[color=blue]
                        > #include <emmintrin.h>
                        > #define M 2
                        > __m128d *s, ts, td;
                        >
                        > double *p = malloc(M*sizeof (*p)); //A
                        > // double p[2]; //B
                        >
                        > if (p==NULL) return 0;
                        >
                        > p[0]=p[1]=1.0;
                        >
                        > printf("%.3f %.3f\n", p[0], p[1]);
                        >
                        > s = (__m128d *)p; //D
                        > if (s != p) return 0;
                        > ts = *s;
                        > td = _mm_add_pd(ts, ts);
                        > *s = td; //C
                        >
                        > printf("%.3f %.3f\n", p[0], p[1]);
                        > }
                        >
                        > Line "C" causes a Segmentation Fault and I don't know why.
                        > If I replace line "A" with line "B" it works correctly.
                        > Something must be wrong with line "D" maybe somebody knows what?[/color]

                        Casts are always wrong, only that they sometimes are the only cure. Here
                        however, you say that it should interpret the address 'p' as a '__m128d*'.
                        'd' only holds so much storage, so why don't you first make sure that
                        there is enough?

                        $include <assert.h>
                        assert( (sizeof *s) <= (M * (sizeof *p)));

                        The fact that it does seem to work with 'B' activated is just luck. Try
                        adding another var after p, and output its value before and after 'C'. The
                        stack area was already allocated to your process, but that doesn't mean
                        you are allowed to use it. The heap is allocated more exactly to fit your
                        needs, crossing these bounds therefore earlier causes the segmentation
                        fault.

                        Oh, while you are at it, could you please remove everything that is not
                        necessary to demonstrate your problem, e.g. the call to _mm_add_pd()?

                        Uli

                        --
                        FAQ: http://ma.rtij.nl/acllc-c++.FAQ.html

                        Comment

                        • Ulrich Eckhardt

                          #13
                          Re: Segmentation Fault

                          Darrell Grainger wrote:[color=blue]
                          > On Tue, 22 Jun 2004, Christian Lande wrote:[color=green]
                          >> *s = td; //C[/color]
                          >
                          > Is this definitely C? The use of C++ style comments makes me wonder.[/color]

                          C allows those comments. It didn't always allow them, but it does now (and
                          many compilers have been accepting it for some time).

                          Uli

                          Comment

                          • Ravi Uday

                            #14
                            Re: Segmentation Fault

                            Comments inline..

                            "Christian Lande" <IIZUXMKQOEHH@s pammotel.com> wrote in message news:<cb9lot$fn 2$04$1@news.t-online.com>...[color=blue]
                            > Hi all,
                            >
                            > can anybody help me with the following code:
                            >
                            > #include <stdio.h>
                            > #include <emmintrin.h>[/color]

                            Never heard of this header. System specific headers are not discussed
                            here..
                            [color=blue]
                            > #define M 2
                            > int main () {
                            > int i;
                            > __m128d *s, ts, td;[/color]

                            /* whats this __m128d ... its not a C datatype that i've known. */[color=blue]
                            >
                            > double *p = malloc(M*sizeof (*p)); //A
                            > // double p[2]; //B
                            >
                            > if (p==NULL) return 0;
                            >
                            > p[0]=p[1]=1.0;
                            >
                            > printf("%.3f %.3f\n", p[0], p[1]);
                            >
                            > s = (__m128d *)p; //D[/color]

                            /* Unless you tell us whats __m128d stands for how can we know
                            whats happ ?*/
                            [color=blue]
                            > if (s != p) return 0;
                            > ts = *s;
                            > td = _mm_add_pd(ts, ts);[/color]

                            /* mm_add_pd.. where is this defined ! I think its in this function
                            the culprit is, there is some inconsistency in the params you have
                            passed. Did you make a typo !!!! */[color=blue]
                            > *s = td; //C
                            >
                            > printf("%.3f %.3f\n", p[0], p[1]);
                            > }[/color]

                            Since main is declared as function that returns 'int' before exiting
                            the function, call return with 0/EXIT_FAILURE..e tc..
                            [color=blue]
                            >
                            > Line "C" causes a Segmentation Fault and I don't know why. If I replace line
                            > "A" with line "B" it works correctly. Something must be wrong with line "D"
                            > maybe somebody knows what?
                            >
                            > Thanks for any help,
                            > ChL
                            >[/color]

                            Its better you post here a compilable code for us to run/exec and test
                            your program. By this we can make our statements/suggestions clear and
                            crisp.

                            Comment

                            • Christian Lande

                              #15
                              Re: Segmentation Fault

                              "CBFalconer " <cbfalconer@yah oo.com> wrote:
                              [color=blue]
                              > If this is not possible you should, in this case, look for a
                              > newsgroup with 'Microsoft' in its name.[/color]

                              I don't understand this suggestion. What should the code in a
                              'Microsoft'-Group, it was coded for a Xeon with Red Hat Linux?
                              [color=blue]
                              > In that case you should
                              > also isolate your non-portable code to a file or two.[/color]

                              For what reason? In most groups attachments are not not gladly seen.

                              Thx for your answer,
                              ChL





                              Comment

                              Working...