Is this code well formed?

Discussion in 'C Programming' started by James, Aug 14, 2010.

  1. James

    James Guest

    Here is a small program the demonstrates the problem I am having with a
    certain compiler:


    #include <stddef.h>
    #include <stdio.h>

    #define ALIGN_OF(t) \
    offsetof(struct { char pad; t type; }, type)

    int main(void)
    {
    printf("ALIGN_OF(short) == %lu\n", (unsigned long)ALIGN_OF(short));

    return 0;
    }


    The code compiles fine on Comeau without any warnings:
    http://www.comeaucomputing.com/tryitout


    However, it fails to compile using EDG C99:
    http://www.dinkumware.com/exam/default.aspx


    I am getting the following errors:

    --------------------

    "sourceFile.c", line 9: error:
    expected an expression
    printf("short == %lu\n", (unsigned long)ALIGN_OF(short));
    ^

    "sourceFile.c", line 9: error:
    expected a ")"
    printf("short == %lu\n", (unsigned long)ALIGN_OF(short));
    ^

    2 errors detected in the compilation of "sourceFile.c".

    --------------------



    What am I doing wrong? Could it possibly be a compiler problem?

    ;^(
     
    James, Aug 14, 2010
    #1
    1. Advertising

  2. James

    Eric Sosman Guest

    On 8/14/2010 5:23 PM, James wrote:
    > Here is a small program the demonstrates the problem I am having with a
    > certain compiler:
    >
    >
    > #include<stddef.h>
    > #include<stdio.h>
    >
    > #define ALIGN_OF(t) \
    > offsetof(struct { char pad; t type; }, type)
    >
    > int main(void)
    > {
    > printf("ALIGN_OF(short) == %lu\n", (unsigned long)ALIGN_OF(short));
    >
    > return 0;
    > }
    >
    >
    > The code compiles fine on Comeau without any warnings:
    > http://www.comeaucomputing.com/tryitout
    >
    >
    > However, it fails to compile using EDG C99:
    > http://www.dinkumware.com/exam/default.aspx
    >
    >
    > I am getting the following errors:
    >
    > --------------------
    >
    > "sourceFile.c", line 9: error:
    > expected an expression
    > printf("short == %lu\n", (unsigned long)ALIGN_OF(short));
    > ^
    >
    > "sourceFile.c", line 9: error:
    > expected a ")"
    > printf("short == %lu\n", (unsigned long)ALIGN_OF(short));
    > ^
    >
    > 2 errors detected in the compilation of "sourceFile.c".
    >
    > --------------------
    >
    >
    >
    > What am I doing wrong? Could it possibly be a compiler problem?


    Could possibly be. I don't see anything wrong with the code,
    but I'm not a 100% perfect code inspector ...

    --
    Eric Sosman
    lid
     
    Eric Sosman, Aug 14, 2010
    #2
    1. Advertising

  3. On Sat, 14 Aug 2010 14:23:51 -0700, "James" <> wrote:

    >Here is a small program the demonstrates the problem I am having with a
    >certain compiler:
    >
    >
    >#include <stddef.h>
    >#include <stdio.h>
    >
    >#define ALIGN_OF(t) \
    > offsetof(struct { char pad; t type; }, type)
    >
    >int main(void)
    >{
    > printf("ALIGN_OF(short) == %lu\n", (unsigned long)ALIGN_OF(short));
    >
    > return 0;
    >}
    >
    >
    >The code compiles fine on Comeau without any warnings:
    >http://www.comeaucomputing.com/tryitout


    Does it produce the correct answer?

    >
    >
    >However, it fails to compile using EDG C99:
    >http://www.dinkumware.com/exam/default.aspx


    Does this implementation provide a macro for offsetof?

    >
    >
    >I am getting the following errors:
    >
    >--------------------
    >
    >"sourceFile.c", line 9: error:
    > expected an expression
    > printf("short == %lu\n", (unsigned long)ALIGN_OF(short));
    > ^


    Something strange happened because several characters within your
    string literal argument seem to have disappeared.

    You might try adding a space just after the cast.

    >
    >"sourceFile.c", line 9: error:
    > expected a ")"
    > printf("short == %lu\n", (unsigned long)ALIGN_OF(short));
    > ^
    >
    >2 errors detected in the compilation of "sourceFile.c".


    --
    Remove del for email
     
    Barry Schwarz, Aug 15, 2010
    #3
  4. "James" <> writes:
    > Here is a small program the demonstrates the problem I am having with a
    > certain compiler:
    >
    >
    > #include <stddef.h>
    > #include <stdio.h>
    >
    > #define ALIGN_OF(t) \
    > offsetof(struct { char pad; t type; }, type)
    >
    > int main(void)
    > {
    > printf("ALIGN_OF(short) == %lu\n", (unsigned long)ALIGN_OF(short));
    >
    > return 0;
    > }
    >
    >
    > The code compiles fine on Comeau without any warnings:
    > http://www.comeaucomputing.com/tryitout
    >
    >
    > However, it fails to compile using EDG C99:
    > http://www.dinkumware.com/exam/default.aspx
    >
    >
    > I am getting the following errors:
    >
    > --------------------
    >
    > "sourceFile.c", line 9: error:
    > expected an expression
    > printf("short == %lu\n", (unsigned long)ALIGN_OF(short));
    > ^
    >
    > "sourceFile.c", line 9: error:
    > expected a ")"
    > printf("short == %lu\n", (unsigned long)ALIGN_OF(short));
    > ^
    >
    > 2 errors detected in the compilation of "sourceFile.c".

    [...]

    The results window says:

    Your code has been compiled with the EDG compiler using
    the Dinkum C99 library from the Dinkum Compleat Libraries package.

    My best guess is that the Dinkum library defines the offsetof() macro in
    a way that doesn't work with the EDG compiler. The definition of
    offsetof() is inherently non-portable.

    --
    Keith Thompson (The_Other_Keith) <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"
     
    Keith Thompson, Aug 15, 2010
    #4
  5. James

    Dirk Zabel Guest

    Am 14.08.2010 23:23, schrieb James:
    > Here is a small program the demonstrates the problem I am having with a
    > certain compiler:
    >
    >
    > #include<stddef.h>
    > #include<stdio.h>
    >
    > #define ALIGN_OF(t) \
    > offsetof(struct { char pad; t type; }, type)
    >
    > int main(void)
    > {
    > printf("ALIGN_OF(short) == %lu\n", (unsigned long)ALIGN_OF(short));
    >
    > return 0;
    > }
    >
    >
    > The code compiles fine on Comeau without any warnings:
    > http://www.comeaucomputing.com/tryitout
    >
    >
    > However, it fails to compile using EDG C99:
    > http://www.dinkumware.com/exam/default.aspx
    >
    >
    > I am getting the following errors:
    >
    > --------------------
    >
    > "sourceFile.c", line 9: error:
    > expected an expression
    > printf("short == %lu\n", (unsigned long)ALIGN_OF(short));
    > ^
    >
    > "sourceFile.c", line 9: error:
    > expected a ")"
    > printf("short == %lu\n", (unsigned long)ALIGN_OF(short));
    > ^
    >
    > 2 errors detected in the compilation of "sourceFile.c".
    >
    > --------------------
    >
    >
    >
    > What am I doing wrong? Could it possibly be a compiler problem?
    >
    > ;^(
    >
    >

    Try to get just the preprocessor output. I always do this if I don't
    understand a compiler message, and surprisingly ofthen the preprocessor
    output reveals the culprit.
    -- Dirk
     
    Dirk Zabel, Aug 16, 2010
    #5
  6. James

    James Guest

    "Keith Thompson" <> wrote in message
    news:...
    > "James" <> writes:
    >> Here is a small program the demonstrates the problem I am having with a
    >> certain compiler:

    [...]
    > The results window says:
    >
    > Your code has been compiled with the EDG compiler using
    > the Dinkum C99 library from the Dinkum Compleat Libraries package.
    >
    > My best guess is that the Dinkum library defines the offsetof() macro in
    > a way that doesn't work with the EDG compiler. The definition of
    > offsetof() is inherently non-portable.


    Something shi%ty is going on with EDG because the following program will not
    compile:


    #include <stdio.h>


    int main(void)
    {
    printf("sizeof(struct { double x; }) == %lu\n",
    (unsigned long)sizeof(struct { double x; }));

    return 0;
    }


    AFAICT, it looks fine to me. However, the shi% compiler barfs up the
    following error:

    --------------------

    "sourceFile.c", line 7: error:
    type definition is not allowed
    (unsigned long)sizeof(struct { double x; }));
    ^

    1 error detected in the compilation of "sourceFile.c".

    --------------------


    And this is using sizeof instead of offsetof()!


    DAMN!!!
     
    James, Aug 16, 2010
    #6
  7. "Barry Schwarz" <> wrote in message
    news:...
    > On Sat, 14 Aug 2010 14:23:51 -0700, "James" <> wrote:
    >
    >>Here is a small program the demonstrates the problem I am having with a
    >>certain compiler:

    [...]
    >>The code compiles fine on Comeau without any warnings:
    >>http://www.comeaucomputing.com/tryitout

    >
    > Does it produce the correct answer?


    I don't use the Comeau or EDG compilers, however, they should produce a
    "compatible" alignment. The question is whether or not it's the minimum
    alignment allowed. AFAICT, the only downside of the `ALIGN_OF()' macro is
    that you might end up wasting space because it did not return the minimum
    alignment for the given type:

    http://groups.google.com/group/comp.lang.c.moderated/msg/0bf610f1573455c1

    [...]
     
    Chris M. Thomasson, Aug 16, 2010
    #7
  8. James

    Ike Naar Guest

    In article <i4cd5t$qhm$>, James <> wrote:
    >Something shi%ty is going on with EDG because the following program will not
    >compile:
    >
    >#include <stdio.h>
    >
    >int main(void)
    >{
    > printf("sizeof(struct { double x; }) == %lu\n",
    > (unsigned long)sizeof(struct { double x; }));
    >
    > return 0;
    >}
    >
    >AFAICT, it looks fine to me. However, the shi% compiler barfs up the
    >following error:
    >
    >--------------------
    >
    >"sourceFile.c", line 7: error:
    > type definition is not allowed
    > (unsigned long)sizeof(struct { double x; }));
    > ^
    >
    >1 error detected in the compilation of "sourceFile.c".
    >
    >--------------------
    >
    >And this is using sizeof instead of offsetof()!
    >
    >DAMN!!!


    Did you compile that program in C++ mode?
    If so, can your try and see what happens if you compile it
    in C89/90 or C99 mode?
    --

    SDF Public Access UNIX System - http://sdf.lonestar.org
     
    Ike Naar, Aug 17, 2010
    #8
  9. James wrote:
    > --------------------
    >
    > "sourceFile.c", line 7: error:
    > type definition is not allowed
    > (unsigned long)sizeof(struct { double x; }));
    > ^
    >
    > 1 error detected in the compilation of "sourceFile.c".
    >
    > --------------------


    Are you using a C or C++ compiler? Does your compiler automatically
    operate in C mode if the source file has .c extension?

    In C++, it is illegal to define types in `sizeof`. In C it is OK.
    --
    Bets regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Aug 17, 2010
    #9
    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. wtsnet
    Replies:
    4
    Views:
    680
    wtsnet
    Nov 25, 2003
  2. Assimalyst
    Replies:
    4
    Views:
    14,101
    Brock Allen
    Jul 25, 2005
  3. mike
    Replies:
    3
    Views:
    14,704
    Karl Seguin
    Nov 24, 2005
  4. Nathan Sokalski

    DataList Creates Tables That Are Not Well-Formed

    Nathan Sokalski, Nov 30, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    374
    Nathan Sokalski
    Nov 30, 2005
  5. Timo Nentwig

    parsing non-well-formed XML (SAX)

    Timo Nentwig, Jun 4, 2004, in forum: Java
    Replies:
    2
    Views:
    863
    Timo Nentwig
    Jun 4, 2004
Loading...

Share This Page