473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Compiler Issue

Hi Folks,

We are encountering the following code issue on compiler susch as "xlc","gcc" but "icc" passes it successfully.

Sample code:
int main(void)
{
typedef unsigned char oratext;
typedef oratext text;

oratext c[2];
oratext * c_ptr = c;

text ** c_pptr;

c_pptr = &(oratext *)c_ptr;
}

Error:
"a.c", line 11.11: 1506-017 (S) Operand of address operator must be an lvalue or function designator.

It seems the compiler is unable to resolve all the addresses in its single pass. If we split this into to steps it works.

Modified code:
int main(void)
{
typedef unsigned char oratext;
typedef oratext text;

oratext c[2];
oratext * c_ptr = c;
text * c_ptr1;

c_ptr1 = (oratext *)c_ptr;

text ** c_pptr;

c_pptr = &c_ptr1;

}

Is this a known issue? are there any other work arounds for such a problem?

TIA

Himanshu
Jun 23 '06 #1
16 4569
cyber citizen wrote:
Hi Folks,

We are encountering the following code issue on compiler susch as
"xlc","gcc" but "icc" passes it successfully.

Sample code:
int main(void)
{
typedef unsigned char oratext;
typedef oratext text;

oratext c[2];
oratext * c_ptr = c;

text ** c_pptr;

c_pptr = &(oratext *)c_ptr;


Why cast to an oratext* when c_ptr already is one?

c_pptr = &c_ptr;

--
Ian Collins.
Jun 23 '06 #2
cyber citizen wrote:
Hi Folks,

We are encountering the following code issue on compiler susch as "xlc","gcc" but "icc" passes it successfully.

Sample code:
int main(void)
{
typedef unsigned char oratext;
typedef oratext text;

oratext c[2];
oratext * c_ptr = c;

text ** c_pptr;

c_pptr = &(oratext *)c_ptr;
}

Error:
"a.c", line 11.11: 1506-017 (S) Operand of address operator must be an lvalue or function designator.

It seems the compiler is unable to resolve all the addresses in its single pass. If we split this into to steps it works.
The Sun compiler gives "warning: a cast does not yield an lvalue".
Same for lint. This suggests to me that the compiler does not think
that there is an address to be resolved. It makes sense if you think
about it. If c is a char for example and I write &(int)c then what
address would I expect to be returned which can be used in a useful
manner as a pointer to int ? I don't think it's possbile.

Of course I noticed that when you write c_pptr = &(oratext *)c_ptr
c_ptr is already a pointer to oratext but so what ? Would you expect
the compiler to notice that the cast is superfluous and treat the whole
expression as c_pptr = &c_ptr ? I wouldn't. Why are you using a cast
anyway ?

By the way the following simpler piece of code yields a similar
warning:

int main() {
int a , *p , **p1 ;

p = &a ;
p1 = &(int *)p ;
return 0 ;
}

It was an unpleasant surprise to me that splint does not give a warning
in either case.
Is this a known issue? are there any other work arounds for such a problem?


I hadn't come across it before but I wasn't surprised. I don't consider
it a
problem either. Just loose the superfluous cast and the "problem" is
solved.

Spiros Bousbouras

Jun 23 '06 #3
Ian Collins wrote:
cyber citizen wrote:
Hi Folks,

We are encountering the following code issue on compiler susch as
"xlc","gcc" but "icc" passes it successfully.

Sample code:
int main(void)
{
typedef unsigned char oratext;
typedef oratext text;

oratext c[2];
oratext * c_ptr = c;

text ** c_pptr;

c_pptr = &(oratext *)c_ptr;

Why cast to an oratext* when c_ptr already is one?

c_pptr = &c_ptr;


I know the type cast there in superfluous. How ever whats the difference in c_pptr = &(oratext *)c_ptr; and _pptr = &c_ptr;

Regards
Himanshu
Jun 23 '06 #4
cyber citizen wrote:
Ian Collins wrote:
cyber citizen wrote:
Hi Folks,

We are encountering the following code issue on compiler susch as
"xlc","gcc" but "icc" passes it successfully.

Sample code:
int main(void)
{
typedef unsigned char oratext;
typedef oratext text;

oratext c[2];
oratext * c_ptr = c;

text ** c_pptr;

c_pptr = &(oratext *)c_ptr;


Why cast to an oratext* when c_ptr already is one?

c_pptr = &c_ptr;


I know the type cast there in superfluous. How ever whats the difference
in c_pptr = &(oratext *)c_ptr; and _pptr = &c_ptr;

The compiler warning says it all, the cast doesn't yield an lvalue.

--
Ian Collins.
Jun 23 '06 #5
cyber citizen said:
Hi Folks,

We are encountering the following code issue on compiler susch as
"xlc","gcc" but "icc" passes it successfully.

Sample code:
int main(void)
{
typedef unsigned char oratext;
typedef oratext text;

oratext c[2];
oratext * c_ptr = c;

text ** c_pptr;

c_pptr = &(oratext *)c_ptr;
}

Error:
"a.c", line 11.11: 1506-017 (S) Operand of address operator must be an
lvalue or function designator.


The address operator is &. It yields an address. Objects have addresses, but
mere values don't. &4, for example, is meaningless. So is &NULL.

(oratext *) is a cast. Casts are nearly always wrong, and the places where
they're right and hardly ever the places you'd expect. This is one of those
wrong places. What you're doing is trying to convert an oratext * into an
oratext * - an utterly pointless conversion, whose sole effect in this case
is to convert an object (which you might reasonably have passed to &) into
a value (which you can't).

The solution is simple: remove the cast.

c_pptr = &c_ptr;
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 23 '06 #6
cyber citizen posted:

c_pptr = &(oratext *)c_ptr;

Do all explicit casts result in an R-value, even if it could conceivably be
possible to yield an L-value? E.g.:

int main(void)
{
int i;
(unsigned)i = 55; /* Never an L-value */
}
Thus, the programmer would have to write:
*(unsigned *)&i = 55;
Jun 23 '06 #7
Frederick Gotham said:
cyber citizen posted:

c_pptr = &(oratext *)c_ptr;

Do all explicit casts result in an R-value,


Yes. (Incidentally, a cast is an explicit conversion, so an explicit cast is
an explicit explicit conversion!)

3.3.4 of C89 says:
"Preceding an expression by a parenthesized type name converts the
value of the expression to the named type."

So it is the value itself that is converted, and this conversion yields a
value, not an object.
even if it could conceivably
be possible to yield an L-value?
Even then.
E.g.:

int main(void)
{
int i;
(unsigned)i = 55; /* Never an L-value */
}

foo.c:6: warning: ANSI C forbids use of cast expressions as lvalues
foo.c:7: warning: control reaches end of non-void function

Thus, the programmer would have to write:
*(unsigned *)&i = 55;


Yes. Or, more simply: i = 55;

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 23 '06 #8
Richard Heathfield wrote:

foo.c:6: warning: ANSI C forbids use of cast expressions as lvalues


Bit of a strange warning, if ANSI C forbids the use of cast expressions
as lvalues, shouldn't this be an error?

Certainly is in C++.

--
Ian Collins.
Jun 23 '06 #9
Ian Collins wrote:
Richard Heathfield wrote:

foo.c:6: warning: ANSI C forbids use of cast expressions as lvalues
Bit of a strange warning, if ANSI C forbids the use of cast expressions
as lvalues, shouldn't this be an error?


I'd imagine it's a constraint violation, which requires a diagnostic,
and "foo.c:6: warning: ANSI C forbids use of cast expressions as lvalues"
looks like a diagnostic to me.

I don't believe the standard distinguishes between "warnings" and "errors",
nor requires [1] that a constraint violation implies that no object code
is generated.
Certainly is in C++.


C++ requires that errors result in compilation failure and no object
module (or whatever the moral equivalent is)?

[1] Except for the #error directive?

--
Chris "seeker" Dollin
"Who do you serve, and who do you trust?" /Crusade/

Jun 23 '06 #10
Chris Dollin said:
Ian Collins wrote:
Richard Heathfield wrote:

foo.c:6: warning: ANSI C forbids use of cast expressions as lvalues
Bit of a strange warning, if ANSI C forbids the use of cast expressions
as lvalues, shouldn't this be an error?


I'd imagine it's a constraint violation, which requires a diagnostic,
and "foo.c:6: warning: ANSI C forbids use of cast expressions as lvalues"
looks like a diagnostic to me.


Correct.
I don't believe the standard distinguishes between "warnings" and
"errors", nor requires [1] that a constraint violation implies that no
object code is generated.
Correct.
Certainly is in C++.


C++ requires that errors result in compilation failure and no object
module (or whatever the moral equivalent is)?


Off-topic.
[1] Except for the #error directive?


Correct.

"Now don't... be... sad... cos three out o' four ain't bad..."

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 23 '06 #11
Chris Dollin <ch**********@hp.com> wrote:
Ian Collins wrote:
Richard Heathfield wrote:

foo.c:6: warning: ANSI C forbids use of cast expressions as lvalues


Bit of a strange warning, if ANSI C forbids the use of cast expressions
as lvalues, shouldn't this be an error?


I'd imagine it's a constraint violation, which requires a diagnostic,
and "foo.c:6: warning: ANSI C forbids use of cast expressions as lvalues"
looks like a diagnostic to me.

I don't believe the standard distinguishes between "warnings" and "errors",
nor requires [1] that a constraint violation implies that no object code
is generated.


True; what it does say is that _if_ a constraint is violated but the
program is (turned into an executable, c.q., and) run, it causes
undefined behaviour if the constraint-violating code is encountered.

Richard
Jun 23 '06 #12
Richard Bos wrote:
Chris Dollin <ch**********@hp.com> wrote:
Ian Collins wrote:
Richard Heathfield wrote:
>
> foo.c:6: warning: ANSI C forbids use of cast expressions as lvalues

Bit of a strange warning, if ANSI C forbids the use of cast expressions
as lvalues, shouldn't this be an error?


I'd imagine it's a constraint violation, which requires a diagnostic,
and "foo.c:6: warning: ANSI C forbids use of cast expressions as lvalues"
looks like a diagnostic to me.

I don't believe the standard distinguishes between "warnings" and "errors",
nor requires [1] that a constraint violation implies that no object code
is generated.


True; what it does say is that _if_ a constraint is violated but the
program is (turned into an executable, c.q., and) run, it causes
undefined behaviour if the constraint-violating code is encountered.


Does it actually say that the behaviour is undefined only if the
constraint-violating code is encountered? I would've thought it would
have u.b. regardless.

Jun 23 '06 #13

cyber citizen wrote:
Hi Folks,

We are encountering the following code issue on compiler susch as "xlc","gcc" but "icc" passes it successfully.
Others have pointed out the problem, but it is perhaps worth seeing
what the "corrected" code does to understand the problem:
Sample code:
int main(void)
{
typedef unsigned char oratext;
typedef oratext text;

oratext c[2];
oratext * c_ptr = c;

text ** c_pptr;

c_pptr = &(oratext *)c_ptr;
}
This is presumably intended to make c_pptr point at c_ptr.
If we split this into to steps it works.

Modified code:
int main(void)
{
typedef unsigned char oratext;
typedef oratext text;

oratext c[2];
oratext * c_ptr = c;
text * c_ptr1;

c_ptr1 = (oratext *)c_ptr;

text ** c_pptr;

c_pptr = &c_ptr1;

}


And this makes c_pptr point at c_ptr1 - it doesn't make c_pptr point at
c_ptr. c_ptr1 isn't the same variable as c_ptr, it's a different
variable which currently has the same value.

If you want to make c_pptr point at c_ptr, you have to take its address
- not the address of something else.

Paul.

Jun 23 '06 #14
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Chris Dollin <ch**********@hp.com> wrote:

[...]
I don't believe the standard distinguishes between "warnings" and "errors",
nor requires [1] that a constraint violation implies that no object code
is generated.


True; what it does say is that _if_ a constraint is violated but the
program is (turned into an executable, c.q., and) run, it causes
undefined behaviour if the constraint-violating code is encountered.


Where does it say that?

See the recent discussion in comp.std.c, "Does a constraint violation
imply undefined behavior?".

--
Keith Thompson (The_Other_Keith) ks***@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.
Jun 23 '06 #15
Chris Dollin wrote:
Ian Collins wrote:

Richard Heathfield wrote:
foo.c:6: warning: ANSI C forbids use of cast expressions as lvalues
Bit of a strange warning, if ANSI C forbids the use of cast expressions
as lvalues, shouldn't this be an error?

I'd imagine it's a constraint violation, which requires a diagnostic,
and "foo.c:6: warning: ANSI C forbids use of cast expressions as lvalues"
looks like a diagnostic to me.

I don't believe the standard distinguishes between "warnings" and "errors",
nor requires [1] that a constraint violation implies that no object code
is generated.

I still can't see why. Maybe my hopes for C99 addressing these issues
were too high.
Certainly is in C++.

C++ requires that errors result in compilation failure and no object
module (or whatever the moral equivalent is)?

It's an error; you can't take the address of a value.

--
Ian Collins.
Jun 23 '06 #16
Keith Thompson <ks***@mib.org> wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Chris Dollin <ch**********@hp.com> wrote:

[...]
I don't believe the standard distinguishes between "warnings" and "errors",
nor requires [1] that a constraint violation implies that no object code
is generated.


True; what it does say is that _if_ a constraint is violated but the
program is (turned into an executable, c.q., and) run, it causes
undefined behaviour if the constraint-violating code is encountered.


Where does it say that?


I keep mixing this up.

It is a "shall" _outside_ a constraint which explicitly causes UB when
violated.

OTOH, code that violates a constraint usually (always?) has no
explicitly defined behaviour, so it has UB by default. For example,
there's a Constraint that says that the size in an array declarator
shall not be zero; and there is no defined behaviour that says what
happens when it _is_ zero; therefore, declaring an array of zero size
has undefined behaviour.

Richard
Jun 26 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dbrown2 | last post by:
I'm trying to understand and document how to install pypar on Win2k. Pypar needs to be installed with some MPI library code and also needs libpython23.a and other files which are not included with...
8
by: jon morgan | last post by:
OK, I'm going to be brave. There is a bug in VS.Net 1.1 that causes random compiler errors. I have raised this issue in posts at least three time in the past couple of months without attracting...
7
by: Tao Wang | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I saw cuj's conformance roundup, but the result is quite old. I think many people like me want to know newer c++ standard conformance test...
14
by: joshc | last post by:
I'm writing some C to be used in an embedded environment and the code needs to be optimized. I have a question about optimizing compilers in general. I'm using GCC for the workstation and Diab...
6
by: David Lack | last post by:
Hi, I recently installed a 60-day trial of .NET 2003 on my development system. I made tests with previous personal projects (which compiled ok with VC6) and some open source files, and keep...
19
by: Jerry | last post by:
I managed to narrow this down to a very simple expression. try this: private void Bug() { bool b = false; Test(3, (b || b) && b && !b); } private void Works() {
12
by: Andrew Schepler | last post by:
When compiled with Visual C++ .NET 2003 (only), the program below aborts as though no matching catch clause is present. If the copy constructor of A is made public, it successfully catches the...
7
by: ndessai | last post by:
Hi All, I discovered following issue with the VC 7.1 compiler regarding the backword compatibility. I created a dll (Redirect.dll) which exposes a function and simply writes some text (say...
159
by: bernard | last post by:
howdy! please recommend a good c compiler. - should be small - should be fast - should come with a good ide - should be inexpensive i am using windows os.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.