[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Understanding 'volatile' in lstrlib.cpp (Lua 5.3)
- From: Roberto Ierusalimschy <roberto@...>
- Date: Wed, 30 Sep 2020 12:02:16 -0300
> There are a few uses of the 'volatile' keyword in lstrlib.c, and I'm
> trying to understand their purpose (lua 5.3).
>
> I care most about the two instances of "volatile Ftypes u;" The other
> one is copywithendian.
In ANSI C, you cannot assign to a field in an union and read from
another field, which is exactly the purpose of Ftypes. The "volatile"
avoids optimizations based on that knowledge, ensuring that the
code executes "as expected": A write to a field actually writes
to that field, and a subsequent read from another field actually
reads from that field.
The ones in 'copywithendian' are to ensure compatibility with
the arguments. Otherwise, we have this kind of warning:
lstrlib.c:1594:30: warning: passing argument 2 of ‘copywithendian’ discards ‘volatile’ qualifier from pointer target type [-Wdiscarded-qualifiers]
copywithendian(buff, u.buff, size, h.islittle);
-- Roberto