Topic: Access to volatile objects applies to non-volatile objects?
Author: Scott Meyers <NeverRead@aristeia.com>
Date: Fri, 26 Feb 2010 17:44:42 CST Raw View
Johannes Schaub (litb) wrote:
> Hello all. What is the required interpretation of this code:
>
> int a = 0;
> int volatile &ra = a;
>
> for(int i = 0; i < 10; i++)
> ra++;
>
> 1.9/8 in n3000 says
>
> "Access to volatile objects are evaluated strictly according to the rules of
> the abstract machine."
>
> The object "a" is not a volatile object. On the other side, we access it by
> a volatile qualified lvalue. So is the compiler allowed to optimize away the
> writes and reads that occur in the loop, assuming it does one write at the
> end, setting "a" to 10?
I certainly hope not, but I don't know if/where in the standard this is addressed.
> Another question closely related:
>
> int volatile &ra = *(int volatile*)malloc(sizeof(int));
> for(int i = 0; i < 10; i++)
> ra++;
>
> What even is the type of the object denoted by "ra"? I can't find any
> information on this. This way of creating an object also isn't listed at
> 1.8/1.
I believe it's because there is no object. The result of the call to malloc is
a pointer to raw memory, just as the result of a call to operator new is a
pointer to raw memory. As you point out, 1.8/1 requires either a definition or
a new expression, both of which specify the type of a chunk of memory. Without
such a type specification, I don't believe there is any object in the memory.
Note that per 18.4.1.1, operator new returns "storage suitably aligned to
represent any object of that size." This reinforces that (1) not all regions of
storage are objects and (2) operator new doesn't return a pointer to an object,
it returns a pointer to storage into which an object can be placed. Although I
don't think the standard spells it out, I think it's reasonable to assume that
malloc behaves similarly in this regard.
Scott
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Sat, 27 Feb 2010 20:34:37 CST Raw View
Scott Meyers wrote:
> Johannes Schaub (litb) wrote:
>> Another question closely related:
>>
>> int volatile &ra = *(int volatile*)malloc(sizeof(int));
>> for(int i = 0; i < 10; i++)
>> ra++;
>>
>> What even is the type of the object denoted by "ra"? I can't find any
>> information on this. This way of creating an object also isn't listed at
>> 1.8/1.
>
> I believe it's because there is no object. The result of the call to
> malloc is a pointer to raw memory, just as the result of a call to
> operator new is a
> pointer to raw memory. As you point out, 1.8/1 requires either a
> definition or
> a new expression, both of which specify the type of a chunk of memory.
> Without such a type specification, I don't believe there is any object in
> the memory.
>
Hmm, but at least at the point where we do "ra++", there has to be an object
(3.10/2 "An lvalue refers to an object or function" for just one example).
Now, the lvalue has type "int volatile" - i think it's reasonable to assume
that the object has that same type.
But as opposed to C, where blank storage area is an object and for the
purpose of access an "effective type" is assumed (objects themself have no
type), in C++ "type" is an object property and determined at creation of the
object according to section 1.8.
To be frank, this question of where and how the object is created and its
type is determined with malloc has been bugging me for a long time now. But
now it's not only a theoretical game of standardese reading anymore, but
actually observable in implementations depending on the treatment of
"volatile" with regard to the object type.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Sun, 21 Feb 2010 09:59:12 CST Raw View
Hello all. What is the required interpretation of this code:
int a = 0;
int volatile &ra = a;
for(int i = 0; i < 10; i++)
ra++;
1.9/8 in n3000 says
"Access to volatile objects are evaluated strictly according to the rules of
the abstract machine."
The object "a" is not a volatile object. On the other side, we access it by
a volatile qualified lvalue. So is the compiler allowed to optimize away the
writes and reads that occur in the loop, assuming it does one write at the
end, setting "a" to 10?
Another question closely related:
int volatile &ra = *(int volatile*)malloc(sizeof(int));
for(int i = 0; i < 10; i++)
ra++;
What even is the type of the object denoted by "ra"? I can't find any
information on this. This way of creating an object also isn't listed at
1.8/1.
Thanks for all pointers!
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]