I think this is a problem in gcc.

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

    I think this is a problem in gcc.

    For some reason, when I compile this code:

    int main(){
    char* a = malloc(5);
    long* l = (*long) a;
    } //I shortened it to isolate the problem.

    I get this error:

    error.c:3: error: expected expression before ‘long’

    But there IS an expression before 'long'. Why is this?
  • Chris McDonald

    #2
    Re: I think this is a problem in gcc.

    Aereshaa <Aereshaa@gmail .comwrites:
    >For some reason, when I compile this code:
    >int main(){
    char* a =3D malloc(5);
    long* l =3D (*long) a;
    >} //I shortened it to isolate the problem.
    >I get this error:
    >error.c:3: error: expected expression before =91long=92
    >But there IS an expression before 'long'. Why is this?
    (long *) instead of (* long) ?

    --
    Chris.

    Comment

    • kongweize@gmail.com

      #3
      Re: I think this is a problem in gcc.

      On Jun 2, 11:38 am, Aereshaa <Aeres...@gmail .comwrote:
      For some reason, when I compile this code:
      >
      int main(){
       char* a = malloc(5);
       long* l = (*long) a;
      >
      } //I shortened it to isolate the problem.
      >
      I get this error:
      >
      error.c:3: error: expected expression before ‘long’
      >
      But there IS an expression before 'long'. Why is this?
      I think you made mistake in
      long* l = (*long) a;

      the * in (*long) is recognized as a multiplication sign

      Comment

      • Aereshaa

        #4
        Re: I think this is a problem in gcc.

        On Jun 1, 11:42 pm, Chris McDonald <ch...@csse.uwa .edu.auwrote:
        Aereshaa <Aeres...@gmail .comwrites:
        For some reason, when I compile this code:
        int main(){
        char* a =3D malloc(5);
        long* l =3D (*long) a;
        } //I shortened it to isolate the problem.
        I get this error:
        error.c:3: error: expected expression before =91long=92
        But there IS an expression before 'long'. Why is this?
        >
        (long *) instead of (* long) ?
        >
        --
        Chris.
        Oh, #it! I was under the impression that it referred to the first
        'long' in the line.
        Thanks.

        Comment

        • vippstar@gmail.com

          #5
          Re: I think this is a problem in gcc.

          On Jun 2, 6:48 am, Aereshaa <Aeres...@gmail .comwrote:
          On Jun 1, 11:42 pm, Chris McDonald <ch...@csse.uwa .edu.auwrote:
          >
          Aereshaa <Aeres...@gmail .comwrites:
          >For some reason, when I compile this code:
          >int main(){
          char* a =3D malloc(5);
          long* l =3D (*long) a;
          >} //I shortened it to isolate the problem.
          >I get this error:
          >error.c:3: error: expected expression before =91long=92
          >But there IS an expression before 'long'. Why is this?
          >
          (long *) instead of (* long) ?
          >
          --
          Chris.
          >
          Oh, #it! I was under the impression that it referred to the first
          'long' in the line.
          Thanks.
          What you probably want:
          l = *(long *)a
          However sizeof (long) is not guaranteed to be less or equal to 5, and
          the contents of a are not initialized.
          Try this instead:
          char *a = malloc(sizeof (long)):
          long l;
          if(a) {
          memset(a, 0, sizeof(long));
          l = *(long*)a;
          }

          Comment

          • Barry Schwarz

            #6
            Re: I think this is a problem in gcc.

            On Sun, 1 Jun 2008 20:48:08 -0700 (PDT), Aereshaa <Aereshaa@gmail .com>
            wrote:
            >On Jun 1, 11:42 pm, Chris McDonald <ch...@csse.uwa .edu.auwrote:
            >Aereshaa <Aeres...@gmail .comwrites:
            >For some reason, when I compile this code:
            >int main(){
            char* a =3D malloc(5);
            long* l =3D (*long) a;
            >} //I shortened it to isolate the problem.
            >I get this error:
            >error.c:3: error: expected expression before =91long=92
            >But there IS an expression before 'long'. Why is this?
            >>
            >(long *) instead of (* long) ?
            >>
            >--
            >Chris.
            >
            >Oh, #it! I was under the impression that it referred to the first
            >'long' in the line.
            >Thanks.
            After fixing the cast as suggested, be aware that if sizeof(long)
            exceeds 5 than any attempt to dereference l yields undefined behavor.
            The same is true for a if sizeof(int) exceeds 5 but that is less
            likely.


            Remove del for email

            Comment

            • Keith Thompson

              #7
              Re: I think this is a problem in gcc.

              vippstar@gmail. com writes:
              On Jun 2, 6:48 am, Aereshaa <Aeres...@gmail .comwrote:
              >On Jun 1, 11:42 pm, Chris McDonald <ch...@csse.uwa .edu.auwrote:
              Aereshaa <Aeres...@gmail .comwrites:
              >For some reason, when I compile this code:
              >int main(){
              char* a = malloc(5);
              long* l = (*long) a;
              >} //I shortened it to isolate the problem.
              >I get this error:
              >error.c:3: error: expected expression before 'long'
              >But there IS an expression before 'long'. Why is this?
              (I fixed some quoted-printable noise in the above.)
              (long *) instead of (* long) ?
              >>
              >Oh, #it! I was under the impression that it referred to the first
              >'long' in the line.
              >Thanks.
              >
              What you probably want:
              l = *(long *)a
              I doubt it, unless he's going to change the code substantially.
              ``l'' is of type long*, not long.
              However sizeof (long) is not guaranteed to be less or equal to 5, and
              the contents of a are not initialized.
              Try this instead:
              char *a = malloc(sizeof (long)):
              long l;
              if(a) {
              memset(a, 0, sizeof(long));
              l = *(long*)a;
              }
              That's a rather roundabout way of writing ``long l = 0;'', which
              doesn't particularly resemble what the OP was trying to do.

              But the OP did say that the posted code was shortened to isolate the
              problem. It's not surprising that a piece of code intended only to
              exhibit a syntax error wouldn't make much sense logically.

              If the OP really is trying to assign the result of malloc(5) to a
              long*, I hope he(?) will post again and learn why it's a bad idea.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • Peter Nilsson

                #8
                Re: I think this is a problem in gcc.

                "kongwe...@gmai l.com" <kongwe...@gmai l.comwrote:
                Aereshaa wrote:
                error.c:3: error: expected expression before ‘long’
                >
                I think you made mistake in
                long* l = (*long) a;
                >
                the * in (*long) is recognized as a multiplication sign
                Not that it matters, but I think it's much more likely
                it's recognised as the indirection operator (unary *).

                --
                Peter

                Comment

                • Ian Collins

                  #9
                  Re: I think this is a problem in gcc.

                  Aereshaa wrote:
                  For some reason, when I compile this code:
                  >
                  int main(){
                  char* a = malloc(5);
                  long* l = (*long) a;
                  } //I shortened it to isolate the problem.
                  >
                  I get this error:
                  >
                  error.c:3: error: expected expression before ‘long’
                  >
                  But there IS an expression before 'long'. Why is this?
                  Ignoring the cast problem, this would be more than a little dangerous on
                  any IPL64 system.

                  --
                  Ian Collins.

                  Comment

                  Working...