Is this code well formed?

J

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?

;^(
 
E

Eric Sosman

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 ...
 
B

Barry Schwarz

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.
 
K

Keith Thompson

James said:
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.
 
D

Dirk Zabel

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
 
J

James

Keith Thompson said:
James said:
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!!!
 
C

Chris M. Thomasson

Barry Schwarz said:
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

[...]
 
I

Ike Naar

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?
 
A

Andrey Tarasevich

James said:
--------------------

"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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top