[comp.sys.ibm.pc] Another msc 5.1 bug

seg@smsdpg.uu.net (Scott Garfinkle) (01/04/89)

I just ran across a neat new (?) bug in MSC5.1:
#line 1 "foo.c"
fun() {
	extern int * const a;
	int * const b = a;

	b[0] = 1;
}

produces "foo.c(5) : error C2166: lval specifies 'const' object"

In other words, you can't use a const pointer to a non-const chunk of memory. 

		Scott E. Garfinkle
		SMS Data Products Group, Inc.
		smsdpg!seg@uunet.uu.net

		<standard disclaimer>

marco@hpmcaa.HP.COM (Marco Dalla-Gasperina) (01/05/89)

/ hpmcaa:comp.sys.ibm.pc / seg@smsdpg.uu.net (Scott Garfinkle) / 10:18 am  Jan  3, 1989 /
I just ran across a neat new (?) bug in MSC5.1:
#line 1 "foo.c"
fun() {
	extern int * const a;
	int * const b = a;

	b[0] = 1;
}

produces "foo.c(5) : error C2166: lval specifies 'const' object"

In other words, you can't use a const pointer to a non-const chunk of memory. 

		Scott E. Garfinkle
		SMS Data Products Group, Inc.
		smsdpg!seg@uunet.uu.net

		<standard disclaimer>
----------

I'm not sure of this, but I remember that I was having trouble with keeping
the evaluation order straight in this case...

int * const a      means the value (*a) is constant 
int const * a      means the value (a) is constant
int const * const a means both (a) and (*a) are constant.

Seems backwards but thats what the manual says!

marco

scjones@sdrc.UUCP (Larry Jones) (01/06/89)

In article <200015@hpmcaa.HP.COM>, marco@hpmcaa.HP.COM (Marco Dalla-Gasperina) writes:
> / hpmcaa:comp.sys.ibm.pc / seg@smsdpg.uu.net (Scott Garfinkle) / 10:18 am  Jan  3, 1989 /
> I just ran across a neat new (?) bug in MSC5.1:
> you can't use a const pointer to a non-const chunk of memory. 
> ----------
> 
> I'm not sure of this, but I remember that I was having trouble with keeping
> the evaluation order straight in this case...
> 
> int * const a      means the value (*a) is constant 
> int const * a      means the value (a) is constant
> int const * const a means both (a) and (*a) are constant.
> 
> Seems backwards but thats what the manual says!

Well, if that's what the manual says, it's wrong!  (As is the
compiler, apparently.)

Check out the bindings of the declarations (const binds just like
* does):

int * const a <=> int * (const a)
   a is const and thus may not change

int const *a <=> int const (*a) <=> const int (*a)
   *a is const and thus may not change

Since near and far bind differently (perversely to my way of
thinking), it wouldn't be surprising if they erroneously treated
const the same way.

----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones@sdrc.uucp
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150                  AT&T: (513) 576-2070
"Save the Quayles" - Mark Russell

rap@olivej.olivetti.com (Robert A. Pease) (01/06/89)

In article <139@smsdpg.uu.net> seg@smsdpg.UUCP (Scott Garfinkle) writes:
|I just ran across a neat new (?) bug in MSC5.1:
|#line 1 "foo.c"
|fun() {
|	extern int * const a;
|	int * const b = a;
|
|	b[0] = 1;
|}
|
|produces "foo.c(5) : error C2166: lval specifies 'const' object"
|
|In other words, you can't use a const pointer to a non-const chunk of memory. 

Hold it!  This is not a const pointer, its a pointer [to/of] a const
identifier.  I suspect that if you change the declaration to,

	int const *b = a;
		or
	const int *b = a;

that it would work correctly.  (My bet is on the second alternative.)
Remember that when trying to figure what this thing is in C, you must read
it from the right to the left.

					Robert A. Pease
{hplabs|fortune|microsoft|amdahal|piramid|tolerant|sun|aimes}!oliveb!rap

seg@smsdpg.uu.net (Scott Garfinkle) (01/06/89)

From article by scjones@sdrc.UUCP (Larry Jones):
> marco@hpmcaa.HP.COM (Marco Dalla-Gasperina) writes:
>>> ... in MSC5.1:
>>> you can't use a const pointer to a non-const chunk of memory. 
---
>> ...
>> Seems backwards but thats what the manual says!
> 
> Well, if that's what the manual says, it's wrong!  (As is the
> compiler, apparently.)


"Only" the compiler is wrong. The MSC Language Reference gets it right
(page 74, example 5).  Of course the support people at Microsoft haven't
called me back yet, so maybe it's all just a dream. :-)
		Scott E. Garfinkle
		SMS Data Products Group, Inc.
		uunet!smsdpg!seg

news@oberon.USC.EDU (USENET News) (01/07/89)

In article <139@smsdpg.uu.net> seg@smsdpg.UUCP (Scott Garfinkle) says:
	>I just ran across a neat new (?) bug in MSC5.1:
	>fun() {
	>	extern int * const a;
	>	int * const b = a;
	>	b[0] = 1;
	>}
	>produces "foo.c(5) : error C2166: lval specifies 'const' object"
	>In other words, you can't use a const pointer to a
	>non-const chunk of memory. 
Scott is right.  This is a bug.

In article <200015@hpmcaa.HP.COM> marco@hpmcaa.HP.COM
(Marco Dalla-Gasperina) says:
	>I'm not sure of this, but...
	>	int * const a      means the value (*a) is constant 
	>	int const * a      means the value (a) is constant
	>Seems backwards but thats what the manual says!
Marco is wrong.  The manual says:
"int const * p" declares a pointer to const,
		cf. sec. 4.4 (p. 62) and sec. 4.4.6 (p. 74), while
"int * const p" declares a constant pointer to int,
		cf. sec. 4.5.5 (p. 81) and sec. 4.7.1 (p. 90).
This is consistent with Harbison & Steele, 2nd ed, sec 11.4.3:
	"int * const const_pointer;
	 const int * pointer_to_const;"
Perhaps intentionally H&S do not use the form "int const *" as
possibly misleading.

Related to this topic, why isn't this example from H&S p.265 illegal?
	const int *p;
	*p = 5;   /* Legal, but ... */

Christopher M. Gould          Internet: gould@physics.usc.edu   (128.125.1.194)
Dept. of Physics              Arpanet:  gould@usc-physics.arpa
Univ. of Southern Calif.      UUCP:     ...{uunet,sdcrdcf}!oberon!physics!gould
Los Angeles, CA 90089-0484    Phone:    (213)743-8521

marco@hpmcaa.HP.COM (Marco Dalla-Gasperina) (01/10/89)

/ hpmcaa:comp.sys.ibm.pc / news@oberon.USC.EDU (USENET News) /  2:34 pm  Jan  6, 1989 /
In article <139@smsdpg.uu.net> seg@smsdpg.UUCP (Scott Garfinkle) says:
	>I just ran across a neat new (?) bug in MSC5.1:
	>fun() {
	>	extern int * const a;
	>	int * const b = a;
	>	b[0] = 1;
	>}
	>produces "foo.c(5) : error C2166: lval specifies 'const' object"
	>In other words, you can't use a const pointer to a
	>non-const chunk of memory. 
Scott is right.  This is a bug.

In article <200015@hpmcaa.HP.COM> marco@hpmcaa.HP.COM
(Marco Dalla-Gasperina) says:
	>I'm not sure of this, but...
	>	int * const a      means the value (*a) is constant 
	>	int const * a      means the value (a) is constant
	>Seems backwards but thats what the manual says!
Marco is wrong.  The manual says:
"int const * p" declares a pointer to const,
		cf. sec. 4.4 (p. 62) and sec. 4.4.6 (p. 74), while
"int * const p" declares a constant pointer to int,
		cf. sec. 4.5.5 (p. 81) and sec. 4.7.1 (p. 90).
This is consistent with Harbison & Steele, 2nd ed, sec 11.4.3:
	"int * const const_pointer;
	 const int * pointer_to_const;"
Perhaps intentionally H&S do not use the form "int const *" as
possibly misleading.

Related to this topic, why isn't this example from H&S p.265 illegal?
	const int *p;
	*p = 5;   /* Legal, but ... */

Christopher M. Gould          Internet: gould@physics.usc.edu   (128.125.1.194)
Dept. of Physics              Arpanet:  gould@usc-physics.arpa
Univ. of Southern Calif.      UUCP:     ...{uunet,sdcrdcf}!oberon!physics!gould
Los Angeles, CA 90089-0484    Phone:    (213)743-8521
----------

Arrrggggh!  I hate it when I'm wrong!  My apologies for incorrectly reading
the manual... I thought it sounded weird when I was writing it but I didn't
pay enough attention to my hackers intuition.

I here that 5% of the adult population in this country is illiterit (er..
ileterate, ilitterate)... can't read. Maybe I'm one of them.

apologies,
marco